-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgoa_test.go
41 lines (38 loc) · 1.12 KB
/
goa_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
package debug
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
goahttp "goa.design/goa/v3/http"
)
func TestAdapt(t *testing.T) {
mux := goahttp.NewMuxer()
adapted := Adapt(mux)
adapted.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK")) // nolint: errcheck
})
adapted.Handle("/foo", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("FOO")) // nolint: errcheck
}))
req := &http.Request{Method: "GET", URL: &url.URL{Path: "/"}}
w := httptest.NewRecorder()
adapted.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("got status %d, expected %d", w.Code, http.StatusOK)
}
if w.Body.String() != "OK" {
t.Errorf("got body %q, expected %q", w.Body.String(), "OK")
}
req = &http.Request{Method: "GET", URL: &url.URL{Path: "/foo"}}
w = httptest.NewRecorder()
adapted.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("got status %d, expected %d", w.Code, http.StatusOK)
}
if w.Body.String() != "FOO" {
t.Errorf("got body %q, expected %q", w.Body.String(), "FOO")
}
}