-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: npm audit fix * feat: Implement Get item end to end * refactor: Rename MediaType middleware to ContentType * feat: Add Accept middleware for checking the Accept header * feat: Add default error in ErrorHandler * test: Add test for ItemsGet * chore: Fix linting issues
- Loading branch information
1 parent
4a58553
commit 1dd9273
Showing
22 changed files
with
458 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"slices" | ||
|
||
"github.com/glass-cms/glasscms/lib/mediatype" | ||
) | ||
|
||
// Accept generates a handler that writes a 415 Unsupported Media Type header | ||
// if the request's Accept header does not match the provided media type. | ||
func Accept(accepted ...string) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Header.Get("Accept") == "" { | ||
// Set default media type of Accept header to application/json | ||
r.Header.Set("Accept", "application/json") | ||
} | ||
|
||
header := r.Header.Get("Accept") | ||
mdt, err := mediatype.Parse(header) | ||
if err != nil { | ||
http.Error(w, "Invalid media type for accept header", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if !slices.Contains(accepted, mdt.MediaType) { | ||
http.Error(w, "Unsupported Accept Media Type", http.StatusNotAcceptable) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package middleware_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/glass-cms/glasscms/lib/middleware" | ||
) | ||
|
||
func Test_Accept(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
acceptHeader string | ||
accepted []string | ||
expected int | ||
}{ | ||
"application/json": { | ||
acceptHeader: "application/json", | ||
accepted: []string{"application/json"}, | ||
expected: http.StatusOK, | ||
}, | ||
"application/json with charset": { | ||
acceptHeader: "application/json; charset=utf-8", | ||
accepted: []string{"application/json"}, | ||
expected: http.StatusOK, | ||
}, | ||
"application/xml": { | ||
acceptHeader: "application/xml", | ||
accepted: []string{"application/xml"}, | ||
expected: http.StatusOK, | ||
}, | ||
"unsupported": { | ||
acceptHeader: "text/plain", | ||
accepted: []string{"application/json"}, | ||
expected: http.StatusNotAcceptable, | ||
}, | ||
"invalid": { | ||
acceptHeader: "text/", | ||
accepted: []string{"application/json"}, | ||
expected: http.StatusBadRequest, | ||
}, | ||
"empty": { | ||
acceptHeader: "", | ||
accepted: []string{"application/json"}, | ||
expected: http.StatusOK, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
req, err := http.NewRequest(http.MethodGet, "/test", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
req.Header.Set("Accept", test.acceptHeader) | ||
|
||
rr := httptest.NewRecorder() | ||
|
||
middleware.Accept(test.accepted...)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})).ServeHTTP(rr, req) | ||
|
||
if rr.Code != test.expected { | ||
t.Errorf("expected %d; got %d", test.expected, rr.Code) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"encoding/xml" | ||
"net/http" | ||
|
||
"github.com/glass-cms/glasscms/lib/mediatype" | ||
) | ||
|
||
// SerializeResponse writes the provided data to the response writer in the | ||
// appropriate media type based on the request's Accept header. | ||
func SerializeResponse[T any](w http.ResponseWriter, r *http.Request, statusCode int, data T) { | ||
w.WriteHeader(statusCode) | ||
|
||
switch r.Header.Get("Accept") { | ||
case mediatype.ApplicationJSON: | ||
w.Header().Set("Content-Type", mediatype.ApplicationJSON) | ||
|
||
if err := json.NewEncoder(w).Encode(data); err != nil { | ||
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError) | ||
} | ||
case mediatype.ApplicationXML: | ||
w.Header().Set("Content-Type", mediatype.ApplicationXML) | ||
|
||
if err := xml.NewEncoder(w).Encode(data); err != nil { | ||
http.Error(w, "Failed to encode XML", http.StatusInternalServerError) | ||
} | ||
default: | ||
http.Error(w, "Unsupported media type", http.StatusNotAcceptable) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.