Skip to content

Commit

Permalink
feat: exported MediaTypeProblemJSON and MediaTypeProblemXML
Browse files Browse the repository at this point in the history
  • Loading branch information
otaxhu committed Nov 16, 2024
1 parent 555f4a6 commit 7dfe224
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
8 changes: 4 additions & 4 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func (p *problemHTTPWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h := w.Header()

switch p.contentType {
case problemJsonContentType:
case MediaTypeProblemJSON:
_ = json.NewEncoder(buf).Encode(p.p)
case problemXmlContentType:
case MediaTypeProblemXML:
buf.WriteString(xml.Header)
_ = xml.NewEncoder(buf).Encode(p.p)
}
Expand All @@ -58,7 +58,7 @@ func (p *problemHTTPWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func ServeXML(p Problem) http.Handler {
return &problemHTTPWrapper{
p: p,
contentType: problemXmlContentType,
contentType: MediaTypeProblemXML,
}
}

Expand All @@ -69,6 +69,6 @@ func ServeXML(p Problem) http.Handler {
func ServeJSON(p Problem) http.Handler {
return &problemHTTPWrapper{
p: p,
contentType: problemJsonContentType,
contentType: MediaTypeProblemJSON,
}
}
10 changes: 5 additions & 5 deletions problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func ParseResponse(res *http.Response) (Problem, error) {

var p Problem

if contentType == problemJsonContentType {
if contentType == MediaTypeProblemJSON {
// Use a MapProblem
p = &MapProblem{}
} else if contentType == problemXmlContentType {
} else if contentType == MediaTypeProblemXML {
// Use RegisteredProblem, gonna lost extension members but it's better than failing
p = &RegisteredProblem{}
} else {
Expand Down Expand Up @@ -103,20 +103,20 @@ func ParseResponse(res *http.Response) (Problem, error) {
func ParseResponseCustom(res *http.Response, p Problem) error {
contentType := res.Header.Get("Content-Type")

if contentType != problemJsonContentType && contentType != problemXmlContentType {
if contentType != MediaTypeProblemJSON && contentType != MediaTypeProblemXML {
return fmt.Errorf("%w: got '%s'", ErrInvalidContentType, contentType)
}

defer res.Body.Close()

if contentType == problemJsonContentType {
if contentType == MediaTypeProblemJSON {

err := json.NewDecoder(res.Body).Decode(p)
if err != nil {
return err
}

} else if contentType == problemXmlContentType {
} else if contentType == MediaTypeProblemXML {

err := xml.NewDecoder(res.Body).Decode(p)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestParseResponse(t *testing.T) {
ExpectedError bool
}{
"JSON: OK": {
InputBody: responseFactory(http.StatusInternalServerError, problemJsonContentType, `
InputBody: responseFactory(http.StatusInternalServerError, MediaTypeProblemJSON, `
{
"type": "about:blank",
"status": 500,
Expand All @@ -110,7 +110,7 @@ func TestParseResponse(t *testing.T) {
ExpectedError: false,
},
"XML: OK": {
InputBody: responseFactory(http.StatusInternalServerError, problemXmlContentType, xml.Header+`
InputBody: responseFactory(http.StatusInternalServerError, MediaTypeProblemXML, xml.Header+`
<problem xmlns="urn:ietf:rfc:7807">
<type>about:blank</type>
<status>500</status>
Expand All @@ -134,7 +134,7 @@ func TestParseResponse(t *testing.T) {
ExpectedError: true,
},
"JSON: Bad Syntax": {
InputBody: responseFactory(http.StatusInternalServerError, problemJsonContentType, `
InputBody: responseFactory(http.StatusInternalServerError, MediaTypeProblemJSON, `
{
"status": 500 // Invalid JSON
}
Expand All @@ -143,7 +143,7 @@ func TestParseResponse(t *testing.T) {
ExpectedError: true,
},
"XML: Bad Syntax": {
InputBody: responseFactory(http.StatusInternalServerError, problemXmlContentType, xml.Header+`
InputBody: responseFactory(http.StatusInternalServerError, MediaTypeProblemXML, xml.Header+`
<problem>
<status>500</status>
No Closing Tag
Expand Down Expand Up @@ -207,8 +207,8 @@ func TestServe(t *testing.T) {

contentType := recorder.Result().Header.Get("Content-Type")

if contentType != problemJsonContentType {
t.Errorf("expected %s, got %s", problemJsonContentType, contentType)
if contentType != MediaTypeProblemJSON {
t.Errorf("expected %s, got %s", MediaTypeProblemJSON, contentType)
}

// TODO: See above, find a way to compact a XML document.
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestEmbeddedParseResponseCustom(t *testing.T) {
ExpectedError bool
}{
"JSON: OK": {
InputBody: responseFactory(http.StatusInternalServerError, problemJsonContentType, `
InputBody: responseFactory(http.StatusInternalServerError, MediaTypeProblemJSON, `
{
"type": "about:blank",
"status": 500,
Expand All @@ -268,7 +268,7 @@ func TestEmbeddedParseResponseCustom(t *testing.T) {
ExpectedError: false,
},
"XML: OK": {
InputBody: responseFactory(http.StatusInternalServerError, problemXmlContentType, xml.Header+`
InputBody: responseFactory(http.StatusInternalServerError, MediaTypeProblemXML, xml.Header+`
<problem xmlns="urn:ietf:rfc:7807">
<type>about:blank</type>
<status>500</status>
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestEmbeddedParseResponseCustom(t *testing.T) {
ExpectedError: true,
},
"JSON: Bad Syntax": {
InputBody: responseFactory(http.StatusInternalServerError, problemJsonContentType, `
InputBody: responseFactory(http.StatusInternalServerError, MediaTypeProblemJSON, `
{
"status": 500 // Invalid JSON
}
Expand All @@ -307,7 +307,7 @@ func TestEmbeddedParseResponseCustom(t *testing.T) {
ExpectedError: true,
},
"XML: Bad Syntax": {
InputBody: responseFactory(http.StatusInternalServerError, problemXmlContentType, xml.Header+`
InputBody: responseFactory(http.StatusInternalServerError, MediaTypeProblemXML, xml.Header+`
<problem>
<status>500</status>
No Closing Tag
Expand Down
10 changes: 8 additions & 2 deletions registered.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package problem

const problemJsonContentType = "application/problem+json"
// Media type for JSON Problem Details
//
// https://datatracker.ietf.org/doc/html/rfc9457#name-iana-considerations
const MediaTypeProblemJSON = "application/problem+json"

const problemXmlContentType = "application/problem+xml"
// Media type for XML Problem Details
//
// https://datatracker.ietf.org/doc/html/rfc9457#name-iana-considerations
const MediaTypeProblemXML = "application/problem+xml"

// Problem details registered members, as those specified in
// https://datatracker.ietf.org/doc/html/rfc9457#name-members-of-a-problem-detail
Expand Down

0 comments on commit 7dfe224

Please sign in to comment.