@@ -10,7 +10,7 @@ describe('Test the HTTPTransport', function () {
10
10
it ( 'should check the action function of an HTTP transport (text/html)' , function ( ) {
11
11
const url = 'http://www.example.com/'
12
12
const link = new document . Link ( url , 'get' )
13
- const transport = new transports . HTTPTransport ( testUtils . mockedFetch ( 'Hello, world' , 'text/html' ) )
13
+ const transport = new transports . HTTPTransport ( null , testUtils . mockedFetch ( 'Hello, world' , 'text/html' ) )
14
14
15
15
return transport . action ( link , decoders )
16
16
. then ( ( res ) => {
@@ -21,7 +21,7 @@ describe('Test the HTTPTransport', function () {
21
21
it ( 'should check the action function of an HTTP transport (json)' , function ( ) {
22
22
const url = 'http://www.example.com/'
23
23
const link = new document . Link ( url , 'get' )
24
- const transport = new transports . HTTPTransport ( testUtils . mockedFetch ( '{"text": "Hello, world"}' , 'application/json' ) )
24
+ const transport = new transports . HTTPTransport ( null , testUtils . mockedFetch ( '{"text": "Hello, world"}' , 'application/json' ) )
25
25
26
26
return transport . action ( link , decoders )
27
27
. then ( res => expect ( res ) . toEqual ( { text : 'Hello, world' } ) )
@@ -40,117 +40,148 @@ describe('Test the HTTPTransport', function () {
40
40
const url = 'http://www.example.com/'
41
41
const fields = [ new document . Field ( 'firstField' , true , 'form' ) , new document . Field ( 'secondField' , true , 'form' ) ]
42
42
const link = new document . Link ( url , 'post' , 'application/x-www-form-urlencoded' , fields )
43
- const transport = new transports . HTTPTransport ( testUtils . echo )
43
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
44
44
const params = {
45
45
firstField : 'hello' ,
46
46
secondField : 'world'
47
47
}
48
48
49
49
return transport . action ( link , decoders , params )
50
50
. then ( res => expect ( res ) . toEqual ( {
51
- body : 'firstField=hello&secondField=world' ,
52
- method : 'post' ,
53
- url : 'http://www.example.com/'
51
+ url : 'http://www.example.com/' ,
52
+
10000
method : 'POST' ,
53
+ headers : {
54
+ 'Content-Type' : 'application/x-www-form-urlencoded'
55
+ } ,
56
+ body : 'firstField=hello&secondField=world'
54
57
} ) )
55
58
} )
56
59
57
60
it ( 'should check the action function of an HTTP transport (network fail)' , function ( ) {
58
61
const url = 'http://www.example.com/'
59
62
const link = new document . Link ( url , 'get' )
60
- const transport = new transports . HTTPTransport ( testUtils . mockedFetch ( 'ERROR' , 'text/html' , 500 ) )
63
+ const transport = new transports . HTTPTransport ( null , testUtils . mockedFetch ( 'ERROR' , 'text/html' , 500 ) )
61
64
62
- expect ( transport . action ( link , decoders ) . then ( ( ) => { } ) . catch ( ( ) => { } ) ) . toThrow ( )
65
+ return transport . action ( link , decoders )
66
+ . catch ( function ( result ) {
67
+ expect ( result . message ) . toEqual ( '500 BAD REQUEST' )
68
+ expect ( result . content ) . toEqual ( 'ERROR' )
69
+ } )
63
70
} )
64
71
65
72
it ( 'should check the action function of an HTTP transport (json) with query params' , function ( ) {
66
73
const url = 'http://www.example.com/'
67
74
const fields = [ new document . Field ( 'page' , false , 'query' ) ]
68
75
const link = new document . Link ( url , 'get' , 'application/json' , fields )
69
- const transport = new transports . HTTPTransport ( testUtils . echo )
76
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
70
77
const params = {
71
78
page : 23
72
79
}
73
80
74
81
return transport . action ( link , decoders , params )
75
82
. then ( ( res ) => {
76
- expect ( res ) . toEqual ( { url : 'http://www.example.com/?page=23' , method : 'get ' } )
83
+ expect ( res ) . toEqual ( { url : 'http://www.example.com/?page=23' , headers : { } , method : 'GET ' } )
77
84
} )
78
85
} )
79
86
80
87
it ( 'should check the action function of an HTTP transport (json) with path params' , function ( ) {
81
88
const url = 'http://www.example.com/{user}/'
82
89
const fields = [ new document . Field ( 'user' , true , 'path' ) ]
83
90
const link = new document . Link ( url , 'get' , 'application/json' , fields )
84
- const transport = new transports . HTTPTransport ( testUtils . echo )
91
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
85
92
const params = {
86
93
user : 23
87
94
}
88
95
89
96
return transport . action ( link , decoders , params )
90
97
. then ( ( res ) => {
91
- expect ( res ) . toEqual ( { url : 'http://www.example.com/23/' , method : 'get ' } )
98
+ expect ( res ) . toEqual ( { url : 'http://www.example.com/23/' , headers : { } , method : 'GET ' } )
92
99
} )
93
100
} )
94
101
95
102
it ( 'should check the action function of an HTTP transport (json) with post request' , function ( ) {
96
103
const url = 'http://www.example.com/'
97
104
const link = new document . Link ( url , 'post' )
98
- const transport = new transports . HTTPTransport ( testUtils . echo )
105
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
106
+
107
+ return transport . action ( link , decoders )
108
+ . then ( ( res ) => {
109
+ expect ( res ) . toEqual ( { url : 'http://www.example.com/' , headers : { } , method : 'POST' } )
110
+ } )
111
+ } )
112
+
113
+ it ( 'CSRF should be included with POST requests.' , function ( ) {
114
+ const url = 'http://www.example.com/'
115
+ const link = new document . Link ( url , 'post' )
116
+ const csrf = { 'X-CSRFToken' : 'abc' }
117
+ const transport = new transports . HTTPTransport ( csrf , testUtils . echo )
118
+
119
+ return transport . action ( link , decoders )
120
+ . then ( ( res ) => {
121
+ expect ( res ) . toEqual ( { url : 'http://www.example.com/' , headers : { 'X-CSRFToken' : 'abc' } , method : 'POST' } )
122
+ } )
123
+ } )
124
+
125
+ it ( 'CSRF should not be included with GET requests.' , function ( ) {
126
+ const url = 'http://www.example.com/'
127
+ const link = new document . Link ( url , 'get' )
128
+ const csrf = { 'X-CSRFToken' : 'abc' }
129
+ const transport = new transports . HTTPTransport ( csrf , testUtils . echo )
99
130
100
131
return transport . action ( link , decoders )
101
132
. then ( ( res ) => {
102
- expect ( res ) . toEqual ( { url : 'http://www.example.com/' , method : 'post ' } )
133
+ expect ( res ) . toEqual ( { url : 'http://www.example.com/' , headers : { } , method : 'GET ' } )
103
134
} )
104
135
} )
105
136
106
137
it ( 'should check the action function of an HTTP transport (json) with post request and form parameters' , function ( ) {
107
138
const url = 'http://www.example.com/'
108
139
const fields = [ new document . Field ( 'hello' , true , 'form' ) ]
109
140
const link = new document . Link ( url , 'post' , 'application/json' , fields )
110
- const transport = new transports . HTTPTransport ( testUtils . echo )
141
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
111
142
const params = {
112
143
hello : 'world'
113
144
}
114
145
115
146
return transport . action ( link , decoders , params )
116
147
. then ( ( res ) => {
117
- expect ( res ) . toEqual ( { url : 'http://www.example.com/' , method : 'post' , body : JSON . stringify ( { hello : 'world' } ) } )
148
+ expect ( res ) . toEqual ( { url : 'http://www.example.com/' , method : 'POST' , headers : { 'Content-Type' : 'application/json' } , body : JSON . stringify ( { hello : 'world' } ) } )
118
149
} )
119
150
} )
120
151
121
152
it ( 'should check the action function of an HTTP transport (json) with post request and a body parameter' , function ( ) {
122
153
const url = 'http://www.example.com/'
123
154
const fields = [ new document . Field ( 'hello' , true , 'body' ) ]
124
155
const link = new document . Link ( url , 'post' , 'application/json' , fields )
125
- const transport = new transports . HTTPTransport ( testUtils . echo )
156
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
126
157
const params = {
127
158
hello : 'world'
128
159
}
129
160
130
161
return transport . action ( link , decoders , params )
131
162
. then ( ( res ) => {
132
- expect ( res ) . toEqual ( { url : 'http://www.example.com/' , method : 'post' , body : JSON . stringify ( 'world' ) } )
163
+ expect ( res ) . toEqual ( { url : 'http://www.example.com/' , method : 'POST' , headers : { 'Content-Type' : 'application/json' } , body : JSON . stringify ( 'world' ) } )
133
164
} )
134
165
} )
135
166
136
167
it ( 'should check the action function of an HTTP transport (json) with missing optional query params' , function ( ) {
137
168
const url = 'http://www.example.com/'
138
169
const fields = [ new document . Field ( 'page' , false , 'query' ) ]
139
170
const link = new document . Link ( url , 'get' , 'application/json' , fields )
140
- const transport = new transports . HTTPTransport ( testUtils . echo )
171
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
141
172
const params = { }
142
173
143
174
return transport . action ( link , decoders , params )
144
175
. then ( ( res ) => {
145
- expect ( res ) . toEqual ( { url : 'http://www.example.com/' , method : 'get ' } )
176
+ expect ( res ) . toEqual ( { url : 'http://www.example.com/' , headers : { } , method : 'GET ' } )
146
177
} )
147
178
} )
148
179
149
180
it ( 'should check the action function of an HTTP transport (json) with missing required parameter' , function ( ) {
150
181
const url = 'http://www.example.com/{user}/'
151
182
const fields = [ new document . Field ( 'user' , true , 'path' ) ]
152
183
const link = new document . Link ( url , 'get' , 'application/json' , fields )
153
- const transport = new transports . HTTPTransport ( testUtils . echo )
184
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
154
185
const params = { }
155
186
156
187
const callTransport = ( ) => transport . action ( link , decoders , params )
@@ -160,7 +191,7 @@ describe('Test the HTTPTransport', function () {
160
191
it ( 'should check the action function of an HTTP transport (json) with unknown paramater' , function ( ) {
161
192
const url = 'http://www.example.com/'
162
193
const link = new document . Link ( url , 'get' )
163
- const transport = new transports . HTTPTransport ( testUtils . echo )
194
+ const transport = new transports . HTTPTransport ( null , testUtils . echo )
164
195
const params = {
165
196
hello : 'world'
166
197
}
0 commit comments