Skip to content

Commit

Permalink
Rewrite newTestServer.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Mar 15, 2024
1 parent 7e47b16 commit b0b27fb
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 48 deletions.
179 changes: 142 additions & 37 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ func TestInvalidRequests(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
req := httptest.NewRequest(test.method, test.target, nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, test.expectedStatus, rr.Code)
})
Expand All @@ -94,23 +98,35 @@ func TestInvalidRequests(t *testing.T) {
func TestServerMeEndpoint(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/Me", nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusNotImplemented, rr.Code)
}

func TestServerResourceDeleteHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodDelete, "/Users/0001", nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusNoContent, rr.Code)
}

func TestServerResourceDeleteHandlerNotFound(t *testing.T) {
req := httptest.NewRequest(http.MethodDelete, "/Users/9999", nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusNotFound, rr.Code)

Expand Down Expand Up @@ -156,7 +172,11 @@ func TestServerResourceGetHandler(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tt.target, nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand Down Expand Up @@ -185,7 +205,11 @@ func TestServerResourceGetHandler(t *testing.T) {
func TestServerResourceGetHandlerNotFound(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/Users/9999", nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusNotFound, rr.Code)

Expand All @@ -210,7 +234,11 @@ func TestServerResourcePatchHandlerFailOnBadType(t *testing.T) {
]
}`))
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

var resource map[string]interface{}
assertUnmarshalNoError(t, json.Unmarshal(rr.Body.Bytes(), &resource))
Expand All @@ -232,7 +260,11 @@ func TestServerResourcePatchHandlerInvalidPath(t *testing.T) {
]
}`))
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusBadRequest, rr.Code)

Expand All @@ -252,14 +284,23 @@ func TestServerResourcePatchHandlerInvalidRemoveOp(t *testing.T) {
]
}`))
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusBadRequest, rr.Code)
}

func TestServerResourcePatchHandlerMapTypeSubAttribute(t *testing.T) {
recorder := httptest.NewRecorder()
newTestServer(t).ServeHTTP(recorder, httptest.NewRequest(http.MethodPatch, "/Users/0001", strings.NewReader(`{
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}

s.ServeHTTP(recorder, httptest.NewRequest(http.MethodPatch, "/Users/0001", strings.NewReader(`{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
"Operations":[
{
Expand All @@ -272,7 +313,7 @@ func TestServerResourcePatchHandlerMapTypeSubAttribute(t *testing.T) {
assertEqualStatusCode(t, http.StatusOK, recorder.Code)

recorder2 := httptest.NewRecorder()
newTestServer(t).ServeHTTP(recorder2, httptest.NewRequest(http.MethodPatch, "/Users/0001", strings.NewReader(`{
s.ServeHTTP(recorder2, httptest.NewRequest(http.MethodPatch, "/Users/0001", strings.NewReader(`{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
"Operations":[
{
Expand Down Expand Up @@ -319,7 +360,11 @@ func TestServerResourcePatchHandlerReturnsNoContent(t *testing.T) {
}
for _, req := range reqs {
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusNoContent, rr.Code)
}
Expand Down Expand Up @@ -358,7 +403,11 @@ func TestServerResourcePatchHandlerValid(t *testing.T) {
]
}`))
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand Down Expand Up @@ -408,7 +457,11 @@ func TestServerResourcePatchHandlerValidPathHasSubAttributes(t *testing.T) {
]
}`))
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)
}
Expand All @@ -424,7 +477,11 @@ func TestServerResourcePatchHandlerValidRemoveOp(t *testing.T) {
]
}`))
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusNoContent, rr.Code)
}
Expand Down Expand Up @@ -468,7 +525,11 @@ func TestServerResourcePostHandlerValid(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, test.target, test.body)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusCreated, rr.Code)

Expand Down Expand Up @@ -498,7 +559,11 @@ func TestServerResourcePostHandlerValid(t *testing.T) {
func TestServerResourcePutHandlerNotFound(t *testing.T) {
req := httptest.NewRequest(http.MethodPut, "/Users/9999", strings.NewReader(`{"userName": "other"}`))
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusNotFound, rr.Code)

Expand Down Expand Up @@ -549,7 +614,11 @@ func TestServerResourcePutHandlerValid(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodPut, test.target, test.body)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand Down Expand Up @@ -595,7 +664,11 @@ func TestServerResourceTypeHandlerValid(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/ResourceTypes/%s", tt.versionPrefix, tt.resourceType), nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand Down Expand Up @@ -625,7 +698,11 @@ func TestServerResourceTypesHandler(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, test.target, nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand All @@ -650,7 +727,11 @@ func TestServerResourceTypesHandler(t *testing.T) {
func TestServerResourcesGetAllHandlerNegativeCount(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/Users?count=-1", nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand All @@ -663,7 +744,11 @@ func TestServerResourcesGetAllHandlerNegativeCount(t *testing.T) {
func TestServerResourcesGetHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/Users", nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()

Check failure on line 747 in handlers_test.go

View workflow job for this annotation

GitHub Actions / test

multiple-value newTestServer() in single-value context
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand All @@ -676,7 +761,11 @@ func TestServerResourcesGetHandler(t *testing.T) {
func TestServerResourcesGetHandlerMaxCount(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/Users?count=20000", nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand All @@ -688,7 +777,11 @@ func TestServerResourcesGetHandlerMaxCount(t *testing.T) {
func TestServerResourcesGetHandlerPagination(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/Users?count=2&startIndex=2", nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand Down Expand Up @@ -726,13 +819,17 @@ func TestServerSchemaEndpointValid(t *testing.T) {
"%s/Schemas/%s", test.versionPrefix, test.schema,
), nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

var s map[string]interface{}
assertUnmarshalNoError(t, json.Unmarshal(rr.Body.Bytes(), &s))
assertEqual(t, test.schema, s["id"].(string))
var m map[string]interface{}
assertUnmarshalNoError(t, json.Unmarshal(rr.Body.Bytes(), &m))
assertEqual(t, test.schema, m["id"].(string))
})
}
}
Expand All @@ -755,7 +852,11 @@ func TestServerSchemasEndpoint(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, test.target, nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand Down Expand Up @@ -791,7 +892,11 @@ func TestServerSchemasEndpointFilter(t *testing.T) {
"/Schemas?%s", params.Encode(),
), nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)

Expand Down Expand Up @@ -819,7 +924,11 @@ func TestServerServiceProviderConfigHandler(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tt.target, nil)
rr := httptest.NewRecorder()
newTestServer(t).ServeHTTP(rr, req)
s, err := newTestServer()
if err != nil {
t.Fatal(err)
}
s.ServeHTTP(rr, req)

assertEqualStatusCode(t, http.StatusOK, rr.Code)
})
Expand Down Expand Up @@ -930,10 +1039,10 @@ func newTestResourceHandler() ResourceHandler {
}
}

func newTestServer(t *testing.T) Server {
func newTestServer() (Server, error) {
userSchema := getUserSchema()
userSchemaExtension := getUserExtensionSchema()
s, err := NewServer(
return NewServer(
&ServerArgs{
ServiceProviderConfig: &ServiceProviderConfig{},
ResourceTypes: []ResourceType{
Expand Down Expand Up @@ -967,8 +1076,4 @@ func newTestServer(t *testing.T) Server {
},
},
)
if err != nil {
t.Fatal(err)
}
return s
}
Loading

0 comments on commit b0b27fb

Please sign in to comment.