-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorujo_test.go
117 lines (103 loc) · 2.38 KB
/
orujo_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
// Copyright 2014 The orujo 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 orujo
import (
"errors"
"log"
"net/http"
"net/http/httptest"
"testing"
)
func TestPipeQuit(t *testing.T) {
want := "h1h2"
result := ""
h1 := func(w http.ResponseWriter, r *http.Request) {
result += "h1"
}
h2 := func(w http.ResponseWriter, r *http.Request) {
result += "h2"
w.WriteHeader(401)
}
h3 := func(w http.ResponseWriter, r *http.Request) {
result += "h3"
}
p := NewPipe(
http.HandlerFunc(h1),
http.HandlerFunc(h2),
http.HandlerFunc(h3),
)
rec := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
log.Fatal(err)
}
p.ServeHTTP(rec, req)
if result != want {
t.Errorf("Pipe(h1, h2, h3)=%s; want=%s", result, want)
}
}
func TestPipeMandatory(t *testing.T) {
want := "h1h2h3"
result := ""
h1 := func(w http.ResponseWriter, r *http.Request) {
result += "h1"
}
h2 := func(w http.ResponseWriter, r *http.Request) {
result += "h2"
w.WriteHeader(401)
}
h3 := func(w http.ResponseWriter, r *http.Request) {
result += "h3"
}
p := NewPipe(
http.HandlerFunc(h1),
http.HandlerFunc(h2),
M(http.HandlerFunc(h3)),
)
rec := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
log.Fatal(err)
}
p.ServeHTTP(rec, req)
if result != want {
t.Errorf("Pipe(h1, h2, M(h3))=%s; want=%s", result, want)
}
}
func TestErrors(t *testing.T) {
want := []error{
errors.New("Err1.1"),
errors.New("Err1.2"),
errors.New("Err2.1"),
errors.New("Err2.3"),
}
var regErrors []error
h1 := func(w http.ResponseWriter, r *http.Request) {
RegisterError(w, errors.New("Err1.1"))
RegisterError(w, errors.New("Err1.2"))
}
h2 := func(w http.ResponseWriter, r *http.Request) {
RegisterError(w, errors.New("Err2.1"))
RegisterError(w, errors.New("Err2.3"))
regErrors = Errors(w)
}
p := NewPipe(
http.HandlerFunc(h1),
http.HandlerFunc(h2),
)
rec := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
log.Fatal(err)
}
p.ServeHTTP(rec, req)
if len(want) != len(regErrors) {
t.Fatalf("len(Errors(w))=%d; want=%d", len(regErrors), len(want))
}
for i := range regErrors {
if regErrors[i].Error() != want[i].Error() {
t.Errorf("Errors(w)[%d]=%s; want=%s", i, regErrors[i], want[i])
}
}
}