-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
43 lines (32 loc) · 948 Bytes
/
middleware_test.go
File metadata and controls
43 lines (32 loc) · 948 Bytes
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
package middleware
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/practicalgo/code/chap6/complex-server/config"
"github.com/practicalgo/code/chap6/complex-server/handlers"
)
func TestPanicMiddleware(t *testing.T) {
b := new(bytes.Buffer)
c := config.InitConfig(b)
m := http.NewServeMux()
handlers.Register(m, c)
h := panicMiddleware(m, c)
r := httptest.NewRequest("GET", "/panic", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
resp := w.Result()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error reading response body: %v", err)
}
if resp.StatusCode != http.StatusInternalServerError {
t.Errorf("Expected response status: %v, Got: %v\n", http.StatusOK, resp.StatusCode)
}
expectedResponseBody := "Unexpected server error occured"
if string(body) != expectedResponseBody {
t.Errorf("Expected response: %s, Got: %s\n", expectedResponseBody, string(body))
}
}