Skip to content

Commit

Permalink
feat(tests): TestMapProblemMarshaling testing that MapProblem marshal…
Browse files Browse the repository at this point in the history
…s correctly to JSON and testing for Bad Syntax errors in ParseResponse... functions
  • Loading branch information
otaxhu committed Nov 8, 2024
1 parent 44be3d0 commit d252590
Showing 1 changed file with 79 additions and 13 deletions.
92 changes: 79 additions & 13 deletions problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -89,7 +88,7 @@ func TestParseResponse(t *testing.T) {
testCases := map[string]struct {
InputBody *http.Response
ExpectedProblem *RegisteredProblem
ExpectedError error
ExpectedError bool
}{
"JSON: OK": {
InputBody: responseFactory(http.StatusInternalServerError, problemJsonContentType, `
Expand All @@ -108,7 +107,7 @@ func TestParseResponse(t *testing.T) {
Detail: "test",
Instance: "/test",
},
ExpectedError: nil,
ExpectedError: false,
},
"XML: OK": {
InputBody: responseFactory(http.StatusInternalServerError, problemXmlContentType, xml.Header+`
Expand All @@ -127,20 +126,40 @@ func TestParseResponse(t *testing.T) {
Detail: "test",
Instance: "/test",
},
ExpectedError: nil,
ExpectedError: false,
},
"Bad Content Type": {
InputBody: responseFactory(http.StatusInternalServerError, "text/plain", "Test plain"),
ExpectedProblem: nil,
ExpectedError: ErrInvalidContentType,
ExpectedError: true,
},
"JSON: Bad Syntax": {
InputBody: responseFactory(http.StatusInternalServerError, problemJsonContentType, `
{
"status": 500 // Invalid JSON
}
`),
ExpectedProblem: nil,
ExpectedError: true,
},
"XML: Bad Syntax": {
InputBody: responseFactory(http.StatusInternalServerError, problemXmlContentType, xml.Header+`
<problem>
<status>500</status>
No Closing Tag
`),
ExpectedProblem: nil,
ExpectedError: true,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
outProblem, err := ParseResponse(tc.InputBody)
if !errors.Is(err, tc.ExpectedError) {
t.Fatalf("expected `%v`, got `%v`", tc.ExpectedError, err)
if tc.ExpectedError && err == nil {
t.Fatalf("expected error to be non-nil, got <nil>")
} else if !tc.ExpectedError && err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}
if err != nil {
return
Expand Down Expand Up @@ -209,7 +228,7 @@ func TestEmbeddedParseResponseCustom(t *testing.T) {
testCases := map[string]struct {
InputBody *http.Response
ExpectedProblem *Embed
ExpectedError error
ExpectedError bool
}{
"JSON: OK": {
InputBody: responseFactory(http.StatusInternalServerError, problemJsonContentType, `
Expand All @@ -234,7 +253,7 @@ func TestEmbeddedParseResponseCustom(t *testing.T) {
Instance: "/test",
},
},
ExpectedError: nil,
ExpectedError: false,
},
"XML: OK": {
InputBody: responseFactory(http.StatusInternalServerError, problemXmlContentType, xml.Header+`
Expand All @@ -259,21 +278,41 @@ func TestEmbeddedParseResponseCustom(t *testing.T) {
Instance: "/test",
},
},
ExpectedError: nil,
ExpectedError: false,
},
"Bad Content Type": {
InputBody: responseFactory(http.StatusInternalServerError, "text/plain", "Test plain"),
ExpectedProblem: nil,
ExpectedError: ErrInvalidContentType,
ExpectedError: true,
},
"JSON: Bad Syntax": {
InputBody: responseFactory(http.StatusInternalServerError, problemJsonContentType, `
{
"status": 500 // Invalid JSON
}
`),
ExpectedProblem: nil,
ExpectedError: true,
},
"XML: Bad Syntax": {
InputBody: responseFactory(http.StatusInternalServerError, problemXmlContentType, xml.Header+`
<problem>
<status>500</status>
No Closing Tag
`),
ExpectedProblem: nil,
ExpectedError: true,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
outProblem := &Embed{}
err := ParseResponseCustom(tc.InputBody, outProblem)
if !errors.Is(err, tc.ExpectedError) {
t.Fatalf("expected `%v`, got `%v`", tc.ExpectedError, err)
if tc.ExpectedError && err == nil {
t.Fatalf("expected error to be non-nil, got <nil>")
} else if !tc.ExpectedError && err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}
if err != nil {
return
Expand Down Expand Up @@ -345,3 +384,30 @@ func TestEmbeddedMarshaling(t *testing.T) {
})
}
}

// IMPORTANT:
//
// Since maps are unordered data structures by nature, this test will only check if
// InputProblem produces valid JSON when marshaled, it will not do a very deep comparison.
func TestMapProblemMarshaling(t *testing.T) {

testCases := map[string]struct {
InputProblem MapProblem
}{
"OK": {
InputProblem: NewMap(http.StatusInternalServerError, "Test"),
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
b, err := json.Marshal(tc.InputProblem)
if err != nil {
t.Fatal(err)
}
if !json.Valid(b) {
t.Fatalf("expected JSON to be valid")
}
})
}
}

0 comments on commit d252590

Please sign in to comment.