-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_err_test.go
50 lines (41 loc) · 1023 Bytes
/
handler_err_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestHandlerErr(t *testing.T) {
t.Run("TestHandlerErr", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
handlerErr(w, req)
res := w.Result()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("Error: TestHandlerErr: io.ReadAll(res.Body): %v", err)
return
}
if res.StatusCode != http.StatusInternalServerError {
t.Errorf("Unexpected: TestHandlerErr: %v", res.StatusCode)
return
}
if res.Header.Get("Content-Type") != "application/json" {
t.Errorf("Unexpected: TestHandlerErr: %v", res.Header.Get("Content-Type"))
return
}
output := struct {
Error string `json:"error"`
}{}
err = json.Unmarshal(body, &output)
if err != nil {
t.Errorf("Error: TestHandlerErr: %v", err)
return
}
if output.Error != "Internal Server Error" {
t.Errorf("Unexpected: TestHandlerErr: %v", output)
return
}
})
}