Skip to content

Commit

Permalink
add handler test for the valid case
Browse files Browse the repository at this point in the history
add handler test for the valid case
  • Loading branch information
notnmeyer committed Oct 20, 2023
1 parent 4a6c11b commit dbaca69
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
package main

import "testing"
import (
"net/http"
"net/http/httptest"
"testing"
)

func TestHandlerValidRequest(t *testing.T) {
expectedContentType := "application/json; charset=utf-8"
expectedResponseBody := `{"foo":"bar"}`
expectedResponseCode := "418" // http.StatusTeapot

// set up the request
req, err := http.NewRequest("POST", "/test", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Add("X-Response-Json", expectedResponseBody)
req.Header.Add("X-Response-Code", expectedResponseCode)

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

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

// response body
if rr.Body.String() != expectedResponseBody {
t.Errorf("handler returned unexpected body: got '%s' want '%s'", rr.Body.String(), expectedResponseBody)
}

// content-type
if rr.Header()["Content-Type"][0] != expectedContentType {
t.Errorf("handler returned wrong content-type: got '%s' wanted '%s'", rr.Header()["Content-Type"][0], expectedContentType)
}
}

// TODO: test invalid x-response-json
// TODO: test invalid x-response-code

func TestValidateResponseCode(t *testing.T) {
valid := map[string][]string{
Expand Down

0 comments on commit dbaca69

Please sign in to comment.