@@ -5,87 +5,152 @@ import (
5
5
"fmt"
6
6
"net/http"
7
7
"os"
8
+ "path/filepath"
8
9
"reflect"
9
10
"testing"
10
11
"time"
11
12
)
12
13
13
- func TestStubRule_ToJson (t * testing.T ) {
14
- newStubRule := NewStubRule ("PATCH" , URLMatching ("/example" ))
15
- expectedRequestBody := fmt .Sprintf (`{"uuid":"%s","id":"%s","request":{"method":"PATCH","urlPattern":"/example"},"response":{"status":200}}` , newStubRule .uuid , newStubRule .uuid )
16
- result , err := json .Marshal (newStubRule )
14
+ const testDataDir = "testdata"
17
15
18
- if err != nil {
19
- t .Fatalf ("StubRole json.Marshal error: %v" , err )
20
- }
21
- if string (result ) != expectedRequestBody {
22
- t .Errorf ("expected requestBody %q; got %q" , expectedRequestBody , string (result ))
16
+ func TestStubRule_ToJson (t * testing.T ) {
17
+ testCases := []struct {
18
+ Name string
19
+ StubRule * StubRule
20
+ ExpectedFileName string
21
+ }{
22
+ {
23
+ Name : "BasicStubRule" ,
24
+ StubRule : NewStubRule ("PATCH" , URLMatching ("/example" )),
25
+ ExpectedFileName : "expected-template-basic.json" ,
26
+ },
27
+ {
28
+ Name : "StubRuleWithScenario" ,
29
+ StubRule : Post (URLPathEqualTo ("/example" )).
30
+ WithHost (EqualTo ("localhost" )).
31
+ WithScheme ("http" ).
32
+ WithPort (8080 ).
33
+ WithQueryParam ("firstName" , EqualTo ("John" ).Or (EqualTo ("Jack" ))).
34
+ WithQueryParam ("lastName" , NotMatching ("Black" )).
35
+ WithQueryParam ("nickname" , EqualToIgnoreCase ("johnBlack" )).
36
+ WithQueryParam ("address" , Includes (EqualTo ("1" ), Contains ("2" ), NotContains ("3" ))).
37
+ WithQueryParam ("id" , Contains ("1" ).And (NotContains ("2" ))).
38
+ WithBodyPattern (EqualToJson (`{"meta": "information"}` , IgnoreArrayOrder , IgnoreExtraElements )).
39
+ WithBodyPattern (Contains ("information" )).
40
+ WithMultipartPattern (
41
+ NewMultipartPattern ().
42
+ WithName ("info" ).
43
+ WithHeader ("Content-Type" , Contains ("charset" )).
44
+ WithBodyPattern (EqualToJson ("{}" , IgnoreExtraElements )),
45
+ ).
46
+ WithBasicAuth ("username" , "password" ).
47
+ WithHeader ("x-absent" , Absent ()).
48
+ WithCookie ("absentcookie" , Absent ()).
49
+ WithHeader ("x-session" , Matching ("^\\ S+@\\ S+$" )).
50
+ WithCookie ("session" , EqualToXml ("<xml>" )).
51
+ WillReturnResponse (
52
+ NewResponse ().
53
+ WithStatus (http .StatusBadRequest ).
54
+ WithHeader ("Content-Type" , "application/json" ).
55
+ WithBody (`{"code": 400, "detail": "detail"}` ).
56
+ WithFault (FaultConnectionResetByPeer ).
57
+ WithFixedDelay (time .Second * 5 ),
58
+ ).
59
+ WithPostServeAction ("webhook" , NewWebhook ().
60
+ WithMethod ("POST" ).
61
+ WithURL ("http://my-target-host/callback" ).
62
+ WithHeader ("Content-Type" , "application/json" ).
63
+ WithBody (`{ "result": "SUCCESS" }` ).
64
+ WithFixedDelay (time .Second )).
65
+ AtPriority (1 ).
66
+ InScenario ("Scenario" ).
67
+ WhenScenarioStateIs ("Started" ).
68
+ WillSetStateTo ("Stopped" ),
69
+ ExpectedFileName : "expected-template-scenario.json" ,
70
+ },
71
+ {
72
+ Name : "StubRuleWithBearerToken_StartsWithMatcher" ,
73
+ StubRule : Post (URLPathEqualTo ("/example" )).
74
+ WithHost (EqualTo ("localhost" )).
75
+ WithScheme ("http" ).
76
+ WithPort (8080 ).
77
+ WithBearerToken (StartsWith ("token" )).
78
+ WillReturnResponse (
79
+ NewResponse ().
80
+ WithStatus (http .StatusOK ),
81
+ ),
82
+ ExpectedFileName : "expected-template-bearer-auth-startsWith.json" ,
83
+ },
84
+ {
85
+ Name : "StubRuleWithBearerToken_EqualToMatcher" ,
86
+ StubRule : Post (URLPathEqualTo ("/example" )).
87
+ WithHost (EqualTo ("localhost" )).
88
+ WithScheme ("http" ).
89
+ WithPort (8080 ).
90
+ WithBearerToken (EqualTo ("token" )).
91
+ WillReturnResponse (
92
+ NewResponse ().
93
+ WithStatus (http .StatusOK ),
94
+ ),
95
+ ExpectedFileName : "expected-template-bearer-auth-equalTo.json" ,
96
+ },
97
+ {
98
+ Name : "StubRuleWithBearerToken_ContainsMatcher" ,
99
+ StubRule : Post (URLPathEqualTo ("/example" )).
100
+ WithHost (EqualTo ("localhost" )).
101
+ WithScheme ("http" ).
102
+ WithPort (8080 ).
103
+ WithBearerToken (Contains ("token" )).
104
+ WillReturnResponse (
105
+ NewResponse ().
106
+ WithStatus (http .StatusOK ),
107
+ ),
108
+ ExpectedFileName : "expected-template-bearer-auth-contains.json" ,
109
+ },
110
+ {
111
+ Name : "StubRuleWithBearerToken_LogicalMatcher" ,
112
+ StubRule : Post (URLPathEqualTo ("/example" )).
113
+ WithHost (EqualTo ("localhost" )).
114
+ WithScheme ("http" ).
115
+ WithPort (8080 ).
116
+ WithBearerToken (EqualTo ("token123" ).And (StartsWith ("token" ))).
117
+ WillReturnResponse (
118
+ NewResponse ().
119
+ WithStatus (http .StatusOK ),
120
+ ),
121
+ ExpectedFileName : "expected-template-bearer-auth-logicalMatcher.json" ,
122
+ },
23
123
}
24
124
25
- postStubRule := Post (URLPathEqualTo ("/example" )).
26
- WithHost (EqualTo ("localhost" )).
27
- WithScheme ("http" ).
28
- WithPort (8080 ).
29
- WithQueryParam ("firstName" , EqualTo ("John" ).Or (EqualTo ("Jack" ))).
30
- WithQueryParam ("lastName" , NotMatching ("Black" )).
31
- WithQueryParam ("nickname" , EqualToIgnoreCase ("johnBlack" )).
32
- WithQueryParam ("address" , Includes (EqualTo ("1" ), Contains ("2" ), NotContains ("3" ))).
33
- WithQueryParam ("id" , Contains ("1" ).And (NotContains ("2" ))).
34
- WithBodyPattern (EqualToJson (`{"meta": "information"}` , IgnoreArrayOrder , IgnoreExtraElements )).
35
- WithBodyPattern (Contains ("information" )).
36
- WithMultipartPattern (
37
- NewMultipartPattern ().
38
- WithName ("info" ).
39
- WithHeader ("Content-Type" , Contains ("charset" )).
40
- WithBodyPattern (EqualToJson ("{}" , IgnoreExtraElements )),
41
- ).
42
- WithBasicAuth ("username" , "password" ).
43
- WithHeader ("x-absent" , Absent ()).
44
- WithCookie ("absentcookie" , Absent ()).
45
- WithHeader ("x-session" , Matching ("^\\ S+@\\ S+$" )).
46
- WithCookie ("session" , EqualToXml ("<xml>" )).
47
- WillReturnResponse (
48
- NewResponse ().
49
- WithStatus (http .StatusBadRequest ).
50
- WithHeader ("Content-Type" , "application/json" ).
51
- WithBody (`{"code": 400, "detail": "detail"}` ).
52
- WithFault (FaultConnectionResetByPeer ).
53
- WithFixedDelay (time .Second * 5 ),
54
- ).
55
- WithPostServeAction ("webhook" , NewWebhook ().
56
- WithMethod ("POST" ).
57
- WithURL ("http://my-target-host/callback" ).
58
- WithHeader ("Content-Type" , "application/json" ).
59
- WithBody (`{ "result": "SUCCESS" }` ).
60
- WithFixedDelay (time .Second )).
61
- AtPriority (1 ).
62
- InScenario ("Scenario" ).
63
- WhenScenarioStateIs ("Started" ).
64
- WillSetStateTo ("Stopped" )
125
+ for _ , tc := range testCases {
126
+ t .Run (tc .Name , func (t * testing.T ) {
127
+ stubRule := tc .StubRule
65
128
66
- rawExpectedRequestBody , err := os .ReadFile ("expected-template.json" )
67
- if err != nil {
68
- t .Fatalf ("failed to read expected-template.json %v" , err )
69
- }
129
+ rawExpectedRequestBody , err := os .ReadFile (filepath . Join ( testDataDir , tc . ExpectedFileName ) )
130
+ if err != nil {
131
+ t .Fatalf ("failed to read expected JSON file %s: %v" , tc . ExpectedFileName , err )
132
+ }
70
133
71
- rawResult , err := json .Marshal (postStubRule )
72
- if err != nil {
73
- t .Fatalf ("StubRole json.Marshal error: %v" , err )
74
- }
134
+ var expected map [string ]interface {}
135
+ err = json .Unmarshal ([]byte (fmt .Sprintf (string (rawExpectedRequestBody ), stubRule .uuid , stubRule .uuid )), & expected )
136
+ if err != nil {
137
+ t .Fatalf ("StubRole json.Unmarshal error: %v" , err )
138
+ }
75
139
76
- var expected map [string ]interface {}
77
- err = json .Unmarshal ([]byte (fmt .Sprintf (string (rawExpectedRequestBody ), postStubRule .uuid , postStubRule .uuid )), & expected )
78
- if err != nil {
79
- t .Fatalf ("StubRole json.Unmarshal error: %v" , err )
80
- }
140
+ rawResult , err := json .Marshal (stubRule )
141
+ if err != nil {
142
+ t .Fatalf ("StubRule json.Marshal error: %v" , err )
143
+ }
81
144
82
- var parsedResult map [string ]interface {}
83
- err = json .Unmarshal (rawResult , & parsedResult )
84
- if err != nil {
85
- t .Fatalf ("StubRole json.Unmarshal error: %v" , err )
86
- }
145
+ var parsedResult map [string ]interface {}
146
+ err = json .Unmarshal (rawResult , & parsedResult )
147
+ if err != nil {
148
+ t .Fatalf ("StubRule json.Unmarshal error: %v" , err )
149
+ }
87
150
88
- if ! reflect .DeepEqual (parsedResult , expected ) {
89
- t .Errorf ("expected requestBody\n %v\n %v" , parsedResult , expected )
151
+ if ! reflect .DeepEqual (parsedResult , expected ) {
152
+ t .Errorf ("expected JSON:\n %v\n actual JSON:\n %v" , parsedResult , expected )
153
+ }
154
+ })
90
155
}
91
156
}
0 commit comments