-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
56 lines (43 loc) · 1.42 KB
/
main_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
51
52
53
54
55
56
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestHelloHandlerGetOK(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost:8082/hello", nil)
w := httptest.NewRecorder()
helloHandler(w, req)
if want, got := http.StatusOK, w.Result().StatusCode; want != got {
t.Fatalf("expected a %d, instead got: %d", want, got)
}
}
func TestHelloHandlerOtherNOK(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "http://localhost:8082/hello", nil)
w := httptest.NewRecorder()
helloHandler(w, req)
if want, got := http.StatusNotFound, w.Result().StatusCode; want != got {
t.Fatalf("expected a %d, instead got: %d", want, got)
}
}
func TestHelloHandlerWrongRoute(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost:8082/", nil)
w := httptest.NewRecorder()
helloHandler(w, req)
if want, got := http.StatusNotFound, w.Result().StatusCode; want != got {
t.Fatalf("expected a %d, instead got: %d", want, got)
}
}
func TestFormHandler(t *testing.T) {
form := url.Values{}
form.Add("name", "name_example")
form.Add("address", "adress_example")
req := httptest.NewRequest(http.MethodPost, "http://localhost:8082/form", strings.NewReader(form.Encode()))
w := httptest.NewRecorder()
formHandler(w, req)
if want, got := http.StatusOK, w.Result().StatusCode; want != got {
t.Fatalf("expected a %d, instead got: %d", want, got)
}
}