2
2
* Copyright © 2017. TIBCO Software Inc.
3
3
* This file is subject to the license terms contained
4
4
* in the license file that is distributed with this file.
5
- */
5
+ */
6
6
package rest
7
7
8
8
import (
9
+ "context"
9
10
"encoding/json"
10
- "fmt"
11
+ "io/ioutil"
12
+ "net"
13
+ "net/http"
14
+ "os"
15
+ "strconv"
16
+ "strings"
11
17
"testing"
12
18
13
19
"github.com/TIBCOSoftware/flogo-contrib/action/flow/test"
14
20
"github.com/TIBCOSoftware/flogo-lib/core/activity"
15
- "io/ioutil"
21
+
22
+ opentracing "github.com/opentracing/opentracing-go"
16
23
)
17
24
18
25
const reqPostStr string = `{
@@ -36,6 +43,82 @@ func getActivityMetadata() *activity.Metadata {
36
43
return activityMetadata
37
44
}
38
45
46
+ func TestMain (m * testing.M ) {
47
+ opentracing .SetGlobalTracer (& opentracing.NoopTracer {})
48
+
49
+ database := make ([]map [string ]interface {}, 0 , 10 )
50
+ http .HandleFunc ("/v2/pet" , func (w http.ResponseWriter , r * http.Request ) {
51
+ if r .Method == http .MethodPost {
52
+ body , err := ioutil .ReadAll (r .Body )
53
+ if err != nil {
54
+ panic (err )
55
+ }
56
+ var pet map [string ]interface {}
57
+ err = json .Unmarshal (body , & pet )
58
+ if err != nil {
59
+ panic (err )
60
+ }
61
+ pet ["id" ] = len (database )
62
+ database = append (database , pet )
63
+ body , err = json .Marshal (pet )
64
+ if err != nil {
65
+ panic (err )
66
+ }
67
+
68
+ _ , err = w .Write (body )
69
+ if err != nil {
70
+ panic (err )
71
+ }
72
+ }
73
+ })
74
+
75
+ http .HandleFunc ("/v2/pet/" , func (w http.ResponseWriter , r * http.Request ) {
76
+ if r .Method == http .MethodGet {
77
+ parts := strings .Split (r .URL .Path , "/" )
78
+ id , err := strconv .Atoi (parts [3 ])
79
+ if err != nil {
80
+ panic (err )
81
+ }
82
+ data , err := json .Marshal (database [id ])
83
+ if err != nil {
84
+ panic (err )
85
+ }
86
+ _ , err = w .Write (data )
87
+ if err != nil {
88
+ panic (err )
89
+ }
90
+ }
91
+ })
92
+
93
+ http .HandleFunc ("/v2/pet/findByStatus" , func (w http.ResponseWriter , r * http.Request ) {
94
+ if r .Method == http .MethodGet {
95
+ query := r .URL .Query ()
96
+ if query ["status" ][0 ] != "ava" {
97
+ panic ("invalid status" )
98
+ }
99
+ data , err := json .Marshal (database [0 ])
100
+ if err != nil {
101
+ panic (err )
102
+ }
103
+ _ , err = w .Write (data )
104
+ if err != nil {
105
+ panic (err )
106
+ }
107
+ }
108
+ })
109
+
110
+ listener , err := net .Listen ("tcp" , ":8080" )
111
+ if err != nil {
112
+ panic (err )
113
+ }
114
+
115
+ go func () {
116
+ http .Serve (listener , nil )
117
+ }()
118
+
119
+ os .Exit (m .Run ())
120
+ }
121
+
39
122
func TestCreate (t * testing.T ) {
40
123
41
124
act := NewActivity (getActivityMetadata ())
@@ -55,20 +138,37 @@ func TestSimplePost(t *testing.T) {
55
138
tc := test .NewTestActivityContext (getActivityMetadata ())
56
139
57
140
//setup attrs
58
- tc .SetInput ("method" , "POST" )
59
- tc .SetInput ("uri" , "http://petstore.swagger.io/v2/pet" )
60
- tc .SetInput ("content" , reqPostStr )
141
+ tc .SetInput (ivMethod , "POST" )
142
+ tc .SetInput (ivURI , "http://localhost:8080/v2/pet" )
143
+ tc .SetInput (ivContent , reqPostStr )
144
+
145
+ span := opentracing .StartSpan ("test" )
146
+ ctx := opentracing .ContextWithSpan (context .Background (), span )
147
+ tc .SetInput (ivTracing , ctx )
61
148
62
149
//eval
63
150
act .Eval (tc )
64
- val := tc .GetOutput ("result" )
151
+ val := tc .GetOutput (ovResult )
65
152
66
- fmt . Printf ("result: %v\n " , val )
153
+ t . Logf ("result: %v\n " , val )
67
154
68
155
res := val .(map [string ]interface {})
69
156
70
157
petID = res ["id" ].(json.Number ).String ()
71
- fmt .Println ("petID:" , petID )
158
+ t .Log ("petID:" , petID )
159
+ if petID != "0" {
160
+ t .Fatal ("invalid pet id" )
161
+ }
162
+
163
+ status := tc .GetOutput (ovStatus )
164
+ if status == nil {
165
+ t .Error ("status is nil" )
166
+ }
167
+
168
+ tracing := tc .GetOutput (ovTracing )
169
+ if tracing == nil {
170
+ t .Error ("tracing is nil" )
171
+ }
72
172
}
73
173
74
174
func TestSimpleGet (t * testing.T ) {
@@ -77,60 +177,103 @@ func TestSimpleGet(t *testing.T) {
77
177
tc := test .NewTestActivityContext (getActivityMetadata ())
78
178
79
179
//setup attrs
80
- tc .SetInput ("method" , "GET" )
81
- tc .SetInput ("uri" , "http://petstore.swagger.io /v2/pet/" + petID )
180
+ tc .SetInput (ivMethod , "GET" )
181
+ tc .SetInput (ivURI , "http://localhost:8080 /v2/pet/" + petID )
82
182
83
183
//eval
84
184
act .Eval (tc )
85
185
86
- val := tc .GetOutput ("result" )
87
- fmt .Printf ("result: %v\n " , val )
88
- }
186
+ val := tc .GetOutput (ovResult )
187
+ t .Logf ("result: %v\n " , val )
89
188
90
- /*
91
- // TODO fix this test
189
+ res := val .(map [string ]interface {})
190
+
191
+ petID = res ["id" ].(json.Number ).String ()
192
+ t .Log ("petID:" , petID )
193
+ if petID != "0" {
194
+ t .Fatal ("invalid pet id" )
195
+ }
196
+ }
92
197
93
198
func TestParamGet (t * testing.T ) {
94
199
95
- act := activity.Get("github.com/TIBCOSoftware/flogo-contrib/activity/rest" )
200
+ act := NewActivity ( getActivityMetadata () )
96
201
tc := test .NewTestActivityContext (act .Metadata ())
97
202
98
203
//setup attrs
99
- tc.SetInput("method" , "GET")
100
- tc.SetInput("uri" , "http://petstore.swagger.io /v2/pet/:id")
204
+ tc .SetInput (ivMethod , "GET" )
205
+ tc .SetInput (ivURI , "http://localhost:8080 /v2/pet/:id" )
101
206
102
207
pathParams := map [string ]string {
103
208
"id" : petID ,
104
209
}
105
- tc.SetInput("pathParams" , pathParams)
210
+ tc .SetInput (ivPathParams , pathParams )
106
211
107
212
//eval
108
213
act .Eval (tc )
109
214
110
- val := tc.GetOutput("result")
111
- fmt.Printf("result: %v\n", val)
215
+ val := tc .GetOutput (ovResult )
216
+ t .Logf ("result: %v\n " , val )
217
+
218
+ res := val .(map [string ]interface {})
219
+
220
+ petID = res ["id" ].(json.Number ).String ()
221
+ t .Log ("petID:" , petID )
222
+ if petID != "0" {
223
+ t .Fatal ("invalid pet id" )
224
+ }
112
225
}
113
- */
114
226
115
227
func TestSimpleGetQP (t * testing.T ) {
116
228
117
229
act := NewActivity (getActivityMetadata ())
118
230
tc := test .NewTestActivityContext (getActivityMetadata ())
119
231
120
232
//setup attrs
121
- tc .SetInput ("method" , "GET" )
122
- tc .SetInput ("uri" , "http://petstore.swagger.io /v2/pet/findByStatus" )
233
+ tc .SetInput (ivMethod , "GET" )
234
+ tc .SetInput (ivURI , "http://localhost:8080 /v2/pet/findByStatus" )
123
235
124
236
queryParams := map [string ]string {
125
237
"status" : "ava" ,
126
238
}
127
- tc .SetInput ("queryParams" , queryParams )
239
+ tc .SetInput (ivQueryParams , queryParams )
128
240
129
241
//eval
130
242
act .Eval (tc )
131
243
132
- val := tc .GetOutput ("result" )
133
- fmt .Printf ("result: %v\n " , val )
244
+ val := tc .GetOutput (ovResult )
245
+ t .Logf ("result: %v\n " , val )
246
+
247
+ res := val .(map [string ]interface {})
248
+
249
+ petID = res ["id" ].(json.Number ).String ()
250
+ t .Log ("petID:" , petID )
251
+ if petID != "0" {
252
+ t .Fatal ("invalid pet id" )
253
+ }
254
+ }
255
+
256
+ func TestGetContentType (t * testing.T ) {
257
+ contentType := getContentType ("test" )
258
+ if contentType != contentTypeTextPlain {
259
+ t .Error ("content type should be " , contentTypeTextPlain )
260
+ }
261
+
262
+ contentType = getContentType (1 )
263
+ if contentType != contentTypeTextPlain {
264
+ t .Error ("content type should be " , contentTypeTextPlain )
265
+ }
266
+
267
+ contentType = getContentType (make ([]int , 1 ))
268
+ if contentType != contentTypeApplicationJSON {
269
+ t .Error ("content type should be " , contentTypeApplicationJSON )
270
+ }
271
+ }
272
+
273
+ func TestMethodIsValid (t * testing.T ) {
274
+ if ! methodIsValid (methodDELETE ) {
275
+ t .Error ("method should be valid" )
276
+ }
134
277
}
135
278
136
279
func TestBuildURI (t * testing.T ) {
@@ -143,7 +286,10 @@ func TestBuildURI(t *testing.T) {
143
286
144
287
newURI := BuildURI (uri , params )
145
288
146
- fmt .Println (newURI )
289
+ t .Log (newURI )
290
+ if newURI != "http://localhost:7070/flow/1234" {
291
+ t .Fatal ("invalid uri" )
292
+ }
147
293
}
148
294
149
295
func TestBuildURI2 (t * testing.T ) {
@@ -157,7 +303,10 @@ func TestBuildURI2(t *testing.T) {
157
303
158
304
newURI := BuildURI (uri , params )
159
305
160
- fmt .Println (newURI )
306
+ t .Log (newURI )
307
+ if newURI != "https://127.0.0.1:7070/flow/1234/test" {
308
+ t .Fatal ("invalid uri" )
309
+ }
161
310
}
162
311
163
312
func TestBuildURI3 (t * testing.T ) {
@@ -170,7 +319,10 @@ func TestBuildURI3(t *testing.T) {
170
319
171
320
newURI := BuildURI (uri , params )
172
321
173
- fmt .Println (newURI )
322
+ t .Log (newURI )
323
+ if newURI != "http://localhost/flow/1234" {
324
+ t .Fatal ("invalid uri" )
325
+ }
174
326
}
175
327
176
328
func TestBuildURI4 (t * testing.T ) {
@@ -184,5 +336,15 @@ func TestBuildURI4(t *testing.T) {
184
336
185
337
newURI := BuildURI (uri , params )
186
338
187
- fmt .Println (newURI )
339
+ t .Log (newURI )
340
+ if newURI != "https://127.0.0.1/flow/1234/test" {
341
+ t .Fatal ("invalid uri" )
342
+ }
343
+ }
344
+
345
+ func TestGetCerts (t * testing.T ) {
346
+ _ , err := getCerts ("./certs" )
347
+ if err != nil {
348
+ t .Error (err )
349
+ }
188
350
}
0 commit comments