Skip to content

Commit

Permalink
Merge pull request #178 from Watson-Sei/main
Browse files Browse the repository at this point in the history
support preflight request
  • Loading branch information
Xemdo authored Oct 12, 2022
2 parents e62dad9 + fd34911 commit 99ede5e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/mock_api/endpoints/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var userMethodsSupported = map[string]bool{
http.MethodDelete: false,
http.MethodPatch: false,
http.MethodPut: true,
"OPTIONS": true,
}

var userScopesByMethod = map[string][]string{
Expand All @@ -27,6 +28,7 @@ var userScopesByMethod = map[string][]string{
http.MethodDelete: {},
http.MethodPatch: {},
http.MethodPut: {"user:edit"},
"OPTIONS": {},
}

type User struct {
Expand Down Expand Up @@ -62,6 +64,8 @@ func (e UsersEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
getUsers(w, r)
case "OPTIONS":
getUsers(w, r)
case http.MethodPut:
putUsers(w, r)
default:
Expand Down
7 changes: 7 additions & 0 deletions internal/mock_api/mock_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,16 @@ func RegisterHandlers(m *http.ServeMux) {
func loggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%v %v", r.Method, r.URL.Path)

w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")

if r.Method == "OPTIONS" {
w.WriteHeader(200)
return
}

next.ServeHTTP(w, r)
})
}

0 comments on commit 99ede5e

Please sign in to comment.