package origin
import (
"errors"
"io"
"net/http"
"reflect"
"strconv"
"strings"
"testing"
)
func TestUserInfoResponse(t *testing.T) {
testUserInfoResponse(t,
"SuccessNew",
200, "text/xml", `10011111111111001111111111test10011111111121001111111112test1`,
[]UserInfo{
{UserID: 1001111111111, PersonaID: "1001111111111", EAID: "test"},
{UserID: 1001111111112, PersonaID: "1001111111112", EAID: "test1"},
}, nil,
)
testUserInfoResponse(t,
"SuccessOld",
200, "text/xml", `2291234567328123456blahblah`,
[]UserInfo{
{UserID: 2291234567, PersonaID: "328123456", EAID: "blahblah"},
}, nil,
)
testUserInfoResponse(t,
"EmptyToken",
200, "text/xml", ``,
nil, ErrOrigin,
)
testUserInfoResponse(t,
"InvalidExpiredToken",
200, "text/xml", ``,
nil, ErrAuthRequired,
)
testUserInfoResponse(t,
"FakeWrongRootElement",
200, "text/xml", ``,
nil, ErrInvalidResponse,
)
testUserInfoResponse(t,
"FakeError",
200, "text/xml", ``,
nil, ErrOrigin,
)
testUserInfoResponse(t,
"FakeBadResponse",
500, "text/plain", `Fake Internal Server Error`,
nil, ErrOrigin,
)
testUserInfoResponse(t,
"FakeInvalidXML",
200, "text/xml", `fake`,
nil, ErrInvalidResponse,
)
}
func testUserInfoResponse(t *testing.T, name string, status int, mime, xml string, v []UserInfo, err error) {
t.Run(name, func(t *testing.T) {
buf, root, err1 := checkResponseXML(&http.Response{
Status: strconv.Itoa(status) + " " + http.StatusText(status),
StatusCode: status,
Body: io.NopCloser(strings.NewReader(xml)),
Header: http.Header{
"Content-Type": {mime},
},
})
if err1 != nil {
if err == nil {
t.Fatalf("expected no error, got %q", err1)
}
if !errors.Is(err1, err) {
t.Fatalf("expected error %q, got %q", err, err1)
}
return
}
ui, err1 := parseUserInfo(buf, root)
if err1 != nil {
if err == nil {
t.Fatalf("expected no error, got %q", err1)
}
if !errors.Is(err1, err) {
t.Fatalf("expected error %q, got %q", err, err1)
}
return
}
if err != nil {
t.Fatalf("expected error %q, got nothing", err)
}
if !reflect.DeepEqual(ui, v) {
t.Errorf("unexpected result %#v", ui)
}
})
}