-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
64 lines (59 loc) · 1.25 KB
/
main_test.go
File metadata and controls
64 lines (59 loc) · 1.25 KB
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
57
58
59
60
61
62
63
64
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestHandler(t *testing.T) {
cases := []struct {
in, out string
}{
{
in: "dennis@golang.org",
out: "$$$",
},
{
in: "dennis",
out: "Hello, World!",
},
}
for _, c := range cases {
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:8080/"+c.in,
nil,
)
if err != nil {
t.Fatalf("could not create request: %v", err)
}
rec := httptest.NewRecorder()
handler(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected status code 200, got %d", rec.Code)
}
if !strings.Contains(rec.Body.String(), c.out) {
t.Errorf("expected response body to be %s, got %s", c.out, rec.Body.String())
}
}
}
func BenchmarkHandler(b *testing.B) {
for i := 0; i < b.N; i++ {
req, err := http.NewRequest(
http.MethodGet,
"http://localhost:8080/dennis@golang.org",
nil,
)
if err != nil {
b.Fatalf("could not create request: %v", err)
}
rec := httptest.NewRecorder()
handler(rec, req)
if rec.Code != http.StatusOK {
b.Errorf("expected status code 200, got %d", rec.Code)
}
if !strings.Contains(rec.Body.String(), "$$$") {
b.Errorf("expected response body to be %s, got %s", "$$$", rec.Body.String())
}
}
}