-
Notifications
You must be signed in to change notification settings - Fork 7
/
server_test.go
100 lines (89 loc) · 2.33 KB
/
server_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
package httpx_test
import (
"crypto/tls"
"net"
"net/http"
"testing"
"github.com/bojanz/httpx"
)
func Test_NewServer(t *testing.T) {
server := httpx.NewServer("", nil)
if server.Addr != ":http" {
t.Errorf("got %v, want :http", server.Addr)
}
if server.Handler != nil {
t.Errorf("got %v, want nil", server.Handler)
}
server = httpx.NewServer("0.0.0.0:8080", http.DefaultServeMux)
if server.Addr != "0.0.0.0:8080" {
t.Errorf("got %v, want 0.0.0.0:8080", server.Addr)
}
if server.Handler != http.DefaultServeMux {
t.Errorf("got %T, want *http.ServeMux", server.Handler)
}
if server.ReadTimeout == 0 {
t.Errorf("ReadTimeout is not set")
}
if server.WriteTimeout == 0 {
t.Errorf("WriteTimeout is not set")
}
if server.IdleTimeout == 0 {
t.Errorf("IdleTimeout is not set")
}
if server.TLSConfig == nil {
t.Errorf("TLSConfig is not set")
}
}
func Test_NewServerTLS(t *testing.T) {
var cert tls.Certificate
server := httpx.NewServerTLS("", cert, nil)
if server.Addr != ":https" {
t.Errorf("got %v, want :https", server.Addr)
}
if server.TLSConfig == nil {
t.Errorf("TLSConfig is not set")
}
if len(server.TLSConfig.Certificates) == 0 {
t.Errorf("Certificates are not set")
}
}
func TestServer_IsTLS(t *testing.T) {
server := httpx.NewServer("", nil)
if server.IsTLS() {
t.Errorf("got true, want false")
}
var cert tls.Certificate
server = httpx.NewServerTLS("", cert, nil)
if !server.IsTLS() {
t.Errorf("got false, want true")
}
}
func TestServer_Listen(t *testing.T) {
// Invalid systemd socket.
server := httpx.NewServer("systemd:myapp-http", nil)
_, err := server.Listen()
if err == nil {
t.Errorf("got nil, want error")
}
want := "listen systemd myapp-http: socket not found"
if err.Error() != want {
t.Errorf("got %q, want %q", err.Error(), want)
}
// Invalid TCP address.
server = httpx.NewServer("INVALID", http.DefaultServeMux)
_, err = server.Listen()
if _, ok := err.(*net.OpError); !ok {
t.Errorf("got %T, want *net.OpError", err)
}
// Valid TCP address.
server = httpx.NewServer("0.0.0.0:8080", http.DefaultServeMux)
ln, err := server.Listen()
if _, ok := ln.(*net.TCPListener); !ok {
t.Errorf("got %T, want *net.TCPListener", ln)
}
if err != nil {
t.Errorf("unexpected error %v", err)
}
}