-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
57 lines (49 loc) · 1.16 KB
/
response_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 http
import (
"io"
"net"
"testing"
"time"
)
const writeTimeout = 5 * time.Second
func TestStatusDescription(t *testing.T) {
tests := []struct {
input int
expected string
}{
{200, "OK"},
{201, "Created"},
{400, "Bad Request"},
{404, "Not Found"},
{408, "Request Timeout"},
{500, "Internal Server Error"},
{504, "Gateway Timeout"},
{1337, ""},
}
for _, v := range tests {
if got := StatusDescription(v.input); got != v.expected {
t.Errorf("Failed test [%d], want %s got %s\n", v.input, v.expected, got)
}
}
}
func TestResponseWriter_Write(t *testing.T) {
serverConn, clientConn := net.Pipe()
defer clientConn.Close()
go func() {
defer serverConn.Close()
rw := NewResponseWriter(serverConn, writeTimeout)
if _, err := rw.Write([]byte("Hello, world!")); err != nil {
t.Errorf("Write error: %s", err)
}
}()
// Read response
in, err := io.ReadAll(clientConn)
if err != nil {
t.Fatalf("failed to read: %s", err)
}
expected := "HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\n\r\nHello, world!"
got := string(in)
if got != expected {
t.Errorf("Write() output = %q, want %q", got, expected)
}
}