1
1
package http
2
2
3
3
import (
4
+ "encoding/json"
4
5
"testing"
5
6
6
- "encoding/json"
7
7
"github.com/stretchr/testify/assert"
8
8
"github.com/stretchr/testify/require"
9
9
"gopkg.in/yaml.v2"
@@ -22,6 +22,138 @@ func TestNewImposterFS(t *testing.T) {
22
22
})
23
23
}
24
24
25
+ func TestImposterFS_FindImposters (t * testing.T ) {
26
+ // Set up
27
+ const expected = 7
28
+ ifs , err := NewImposterFS ("test/testdata/imposters" )
29
+ require .NoError (t , err )
30
+
31
+ // We trigger the imposters search.
32
+ // We expect exactly [expected] imposters.
33
+ ch := make (chan []Imposter , expected )
34
+ err = ifs .FindImposters (ch )
35
+ require .NoError (t , err )
36
+
37
+ // We collect all the imposters.
38
+ received := make ([]Imposter , 0 , expected )
39
+ for ii := range ch {
40
+ received = append (received , ii ... )
41
+ }
42
+ require .Len (t , received , expected )
43
+
44
+ // Imposter 1
45
+ schemaFile := "schemas/create_gopher_request.json"
46
+ bodyFile := "responses/create_gopher_response.json"
47
+ assert .EqualValues (t , Imposter {
48
+ BasePath : "test/testdata/imposters" ,
49
+ Path : "create_gopher.imp.json" ,
50
+ Request : Request {
51
+ Method : "POST" ,
52
+ Endpoint : "/gophers" ,
53
+ SchemaFile : & schemaFile ,
54
+ Params : & map [string ]string {
55
+ "gopherColor" : "{v:[a-z]+}" ,
56
+ },
57
+ Headers : & map [string ]string {
58
+ "Content-Type" : "application/json" ,
59
+ },
60
+ },
61
+ Response : Responses {{
62
+ Status : 200 ,
63
+ Headers : & map [string ]string {
64
+ "Content-Type" : "application/json" ,
65
+ },
66
+ BodyFile : & bodyFile ,
67
+ }},
68
+ }, received [0 ])
69
+
70
+ // Imposter 2
71
+ assert .EqualValues (t , Imposter {
72
+ BasePath : "test/testdata/imposters" ,
73
+ Path : "create_gopher.imp.json" ,
74
+ Request : Request {},
75
+ }, received [1 ])
76
+
77
+ // Imposter 3
78
+ assert .EqualValues (t , Imposter {
79
+ BasePath : "test/testdata/imposters" ,
80
+ Path : "test_request.imp.json" ,
81
+ Request : Request {
82
+ Method : "GET" ,
83
+ Endpoint : "/testRequest" ,
84
+ },
85
+ Response : Responses {{
86
+ Status : 200 ,
87
+ Body : "Handled" ,
88
+ }},
89
+ }, received [2 ])
90
+
91
+ // Imposter 4
92
+ assert .EqualValues (t , Imposter {
93
+ BasePath : "test/testdata/imposters" ,
94
+ Path : "test_request.imp.yaml" ,
95
+ Request : Request {
96
+ Method : "GET" ,
97
+ Endpoint : "/yamlTestRequest" ,
98
+ },
99
+ Response : Responses {{
100
+ Status : 200 ,
101
+ Body : "Yaml Handled" ,
102
+ }},
103
+ }, received [3 ])
104
+
105
+ // Imposter 5
106
+ assert .EqualValues (t , Imposter {
107
+ BasePath : "test/testdata/imposters" ,
108
+ Path : "test_request.imp.yml" ,
109
+ Request : Request {
110
+ Method : "GET" ,
111
+ Endpoint : "/ymlTestRequest" ,
112
+ },
113
+ Response : Responses {{
114
+ Status : 200 ,
115
+ Body : "Yml Handled" ,
116
+ Delay : ResponseDelay {
117
+ delay : 1000000000 ,
118
+ offset : 4000000000 ,
119
+ },
120
+ }},
121
+ }, received [4 ])
122
+
123
+ // Imposter 6
124
+ assert .EqualValues (t , Imposter {
125
+ BasePath : "test/testdata/imposters" ,
126
+ Path : "test_request.imp.yml" ,
127
+ Request : Request {
128
+ Method : "POST" ,
129
+ Endpoint : "/yamlGophers" ,
130
+ Headers : & map [string ]string {
131
+ "Content-Type" : "application/json" ,
132
+ },
133
+ },
134
+ Response : Responses {{
135
+ Status : 201 ,
136
+ Headers : & map [string ]string {
137
+ "Content-Type" : "application/json" ,
138
+ "X-Source" : "YAML" ,
139
+ },
140
+ BodyFile : & bodyFile ,
141
+ }},
142
+ }, received [5 ])
143
+
144
+ // Imposter 7
145
+ assert .EqualValues (t , Imposter {
146
+ BasePath : "test/testdata/imposters" ,
147
+ Path : "test_request.imp.yml" ,
148
+ Request : Request {},
149
+ }, received [6 ])
150
+
151
+ // Finally, once the search is done,
152
+ // the channel must be closed.
153
+ _ , open := <- ch
154
+ require .False (t , open )
155
+ }
156
+
25
157
func TestResponses_MarshalJSON (t * testing.T ) {
26
158
tcs := map [string ]struct {
27
159
rr * Responses
0 commit comments