This repository was archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathtan_test.go
119 lines (99 loc) · 2.32 KB
/
tan_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tango
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"time"
)
func TestTan1(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
l := NewLogger(os.Stdout)
o := Classic(l)
o.Get("/", func() string {
return Version()
})
o.Logger().Debug("it's ok")
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), Version())
}
func TestTan2(t *testing.T) {
o := Classic()
o.Get("/", func() string {
return Version()
})
go o.Run()
time.Sleep(100 * time.Millisecond)
resp, err := http.Get("http://localhost:8000/")
if err != nil {
t.Error(err)
}
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
expect(t, resp.StatusCode, http.StatusOK)
expect(t, string(bs), Version())
}
func TestTan3(t *testing.T) {
o := Classic()
o.Get("/", func() string {
return Version()
})
go o.Run(":4040")
time.Sleep(100 * time.Millisecond)
resp, err := http.Get("http://localhost:4040/")
if err != nil {
t.Error(err)
}
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
expect(t, resp.StatusCode, http.StatusOK)
expect(t, string(bs), Version())
}
/*
func TestTan4(t *testing.T) {
o := Classic()
o.Get("/", func() string {
return Version()
})
go o.RunTLS("./public/cert.pem", "./public/key.pem", ":5050")
time.Sleep(100 * time.Millisecond)
resp, err := http.Get("https://localhost:5050/")
if err != nil {
t.Error(err)
}
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
expect(t, resp.StatusCode, http.StatusOK)
expect(t, string(bs), Version())
}*/
/* Test Helpers */
func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
func refute(t *testing.T, a interface{}, b interface{}) {
if a == b {
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}