Skip to content

Commit

Permalink
Merge branch 'main' into feat/multi-arch-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
notnmeyer committed Jan 10, 2024
2 parents 802804e + ca00fc8 commit 72946ae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ func main() {
}

func handler(w http.ResponseWriter, r *http.Request) {
responseBody, responseCode := buildResponse(r.Header)

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(responseCode)
w.Write([]byte(responseBody))
switch r.Method {
case "OPTIONS":
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Max-Age", "86400")
w.WriteHeader(http.StatusNoContent)
default:
responseBody, responseCode := buildResponse(r.Header)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(responseCode)
w.Write([]byte(responseBody))
}
}

func buildResponse(header map[string][]string) (string, int) {
Expand Down
20 changes: 20 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ func TestHandlerWithInvalidXResponseCode(t *testing.T) {
}
}

func TestHandlerPreflightRequest(t *testing.T) {
expectedResponseCode := http.StatusNoContent

// set up the request
req, err := http.NewRequest("OPTIONS", "/test", nil)
if err != nil {
t.Fatal(err)
}

// make the request
rr := httptest.NewRecorder()
h := http.HandlerFunc(handler)
h.ServeHTTP(rr, req)

// status code
if status := rr.Code; status != expectedResponseCode {
t.Errorf("handler returned wrong status code: got '%d' want '%d'", status, expectedResponseCode)
}
}

func TestValidateResponseCode(t *testing.T) {
valid := map[string][]string{
"X-Response-Code": {"201"},
Expand Down

0 comments on commit 72946ae

Please sign in to comment.