-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
57 lines (40 loc) · 890 Bytes
/
http_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
57
package server
import (
"context"
"io"
"log"
"net/http"
"testing"
)
func testHandler() http.Handler {
fn := func(rsp http.ResponseWriter, req *http.Request) {
rsp.Write([]byte("Hello world"))
}
h := http.HandlerFunc(fn)
return h
}
func TestHTTPServer(t *testing.T) {
ctx := context.Background()
s, err := NewServer(ctx, "http://localhost:8080")
if err != nil {
t.Fatalf("Failed to create server, %v", err)
}
go func() {
err := s.ListenAndServe(ctx, testHandler())
if err != nil {
log.Fatalf("Failed to start server, %v", err)
}
}()
rsp, err := http.Get("http://localhost:8080")
if err != nil {
t.Fatalf("Failed to GET request, %v", err)
}
defer rsp.Body.Close()
body, err := io.ReadAll(rsp.Body)
if err != nil {
t.Fatalf("Failed to read response, %v", err)
}
if string(body) != "Hello world" {
t.Fatalf("Unexpected response")
}
}