-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RequestSpecificationImpl.groovy
2173 lines (1834 loc) · 78.2 KB
/
RequestSpecificationImpl.groovy
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.restassured.internal
import io.restassured.RestAssured
import io.restassured.authentication.AuthenticationScheme
import io.restassured.authentication.CertAuthScheme
import io.restassured.authentication.FormAuthScheme
import io.restassured.authentication.NoAuthScheme
import io.restassured.config.*
import io.restassured.filter.Filter
import io.restassured.filter.OrderedFilter
import io.restassured.filter.log.RequestLoggingFilter
import io.restassured.filter.log.ResponseLoggingFilter
import io.restassured.filter.time.TimingFilter
import io.restassured.http.*
import io.restassured.internal.MapCreator.CollisionStrategy
import io.restassured.internal.filter.CsrfFilter
import io.restassured.internal.filter.FilterContextImpl
import io.restassured.internal.filter.FormAuthFilter
import io.restassured.internal.filter.SendRequestFilter
import io.restassured.internal.http.*
import io.restassured.internal.log.LogRepository
import io.restassured.internal.mapping.ObjectMapperSerializationContextImpl
import io.restassured.internal.mapping.ObjectMapping
import io.restassured.internal.multipart.MultiPartInternal
import io.restassured.internal.multipart.MultiPartSpecificationImpl
import io.restassured.internal.multipart.RestAssuredMultiPartEntity
import io.restassured.internal.proxy.RestAssuredProxySelector
import io.restassured.internal.proxy.RestAssuredProxySelectorRoutePlanner
import io.restassured.internal.support.ParameterUpdater
import io.restassured.internal.support.PathSupport
import io.restassured.mapper.ObjectMapper
import io.restassured.mapper.ObjectMapperType
import io.restassured.response.Response
import io.restassured.specification.*
import io.restassured.spi.AuthFilter
import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.CredentialsProvider
import org.apache.http.client.HttpClient
import org.apache.http.entity.mime.FormBodyPartBuilder
import org.apache.http.impl.client.AbstractHttpClient
import org.apache.http.impl.client.BasicCredentialsProvider
import java.nio.charset.StandardCharsets
import java.security.KeyStore
import java.util.Map.Entry
import java.util.regex.Matcher
import java.util.regex.Pattern
import static io.restassured.config.ParamConfig.UpdateStrategy.REPLACE
import static io.restassured.http.ContentType.*
import static io.restassured.http.Method.*
import static io.restassured.internal.common.assertion.AssertParameter.notNull
import static io.restassured.internal.serialization.SerializationSupport.isSerializableCandidate
import static io.restassured.internal.support.PathSupport.isFullyQualified
import static io.restassured.internal.support.PathSupport.mergeAndRemoveDoubleSlash
import static java.lang.String.format
import static java.util.Arrays.asList
import static org.apache.commons.lang3.StringUtils.*
import static org.apache.http.client.params.ClientPNames.*
class RequestSpecificationImpl implements FilterableRequestSpecification, GroovyInterceptable {
private static final int DEFAULT_HTTP_TEST_PORT = 8080
private static final String CONTENT_TYPE = "Content-Type"
private static final String DOUBLE_SLASH = "//"
private static final String LOCALHOST = "localhost"
private static final String CHARSET = "charset"
private static final String ACCEPT_HEADER_NAME = "Accept"
private static final String SSL = "SSL"
private static final String MULTIPART = "multipart"
private static final String MULTIPART_CONTENT_TYPE_PREFIX_WITH_SLASH = MULTIPART + "/"
private static final String MULTIPART_CONTENT_TYPE_PREFIX_WITH_PLUS = MULTIPART + "+"
private static final String APPLICATION_JSON = "application/json"
private static final String TEMPLATE_START = "{"
private static final String TEMPLATE_END = "}"
private String baseUri
private String path = ""
private String method
private String basePath
// If first argument is null it means that it's a redundant path param that cannot be mapped to a placeholder
// If second argument is null it means that the parameter has been removed (but we keep it to retain order)
private List<Tuple2<String, String>> unnamedPathParamsTuples = new ArrayList<>()
private AuthenticationScheme defaultAuthScheme
private int port
private Map<String, String> requestParameters = new LinkedHashMap()
private Map<String, String> queryParameters = new LinkedHashMap()
private Map<String, String> formParameters = new LinkedHashMap()
private Map<String, String> namedPathParameters = [:]
private Map<String, String> httpClientParams = [:]
AuthenticationScheme authenticationScheme = new NoAuthScheme()
private FilterableResponseSpecification responseSpecification
private Headers requestHeaders = new Headers([])
private Cookies cookies = new Cookies([])
private Object requestBody
private List<Filter> filters = []
private boolean urlEncodingEnabled
private RestAssuredConfig restAssuredConfig
private List<MultiPartInternal> multiParts = []
private ParameterUpdater parameterUpdater = new ParameterUpdater(new ParameterUpdater.Serializer() {
String serializeIfNeeded(Object value) {
return RequestSpecificationImpl.this.serializeIfNeeded(value)
}
})
private ProxySpecification proxySpecification = null
private LogRepository logRepository
// This field should be removed once http://jira.codehaus.org/browse/GROOVY-4647 is resolved, merge with sha 9619c3b when it's fixed.
private AbstractHttpClient httpClient
private boolean allowContentType
private boolean addCsrfFilter
RequestSpecificationImpl(String baseURI, int requestPort, String basePath, AuthenticationScheme defaultAuthScheme, List<Filter> filters,
RequestSpecification defaultSpec, boolean urlEncode, RestAssuredConfig restAssuredConfig, LogRepository logRepository,
ProxySpecification proxySpecification, boolean allowContentType, boolean addCsrfFilter) {
notNull(baseURI, "baseURI")
notNull(basePath, "basePath")
notNull(defaultAuthScheme, "defaultAuthScheme")
notNull(filters, "Filters")
notNull(urlEncode, "URL Encode query params option")
this.baseUri = baseURI
this.basePath = basePath
this.defaultAuthScheme = defaultAuthScheme
this.filters.addAll(filters)
this.urlEncodingEnabled = urlEncode
port(requestPort)
this.restAssuredConfig = restAssuredConfig
if (defaultSpec != null) {
spec(defaultSpec)
}
this.logRepository = logRepository
this.proxySpecification = proxySpecification
this.allowContentType = allowContentType
this.addCsrfFilter = addCsrfFilter
}
RequestSpecification when() {
return this
}
RequestSpecification given() {
return this
}
RequestSpecification that() {
return this
}
ResponseSpecification response() {
return responseSpecification
}
Response get(String path, Object... pathParams) {
applyPathParamsAndSendRequest(GET, path, pathParams)
}
Response post(String path, Object... pathParams) {
applyPathParamsAndSendRequest(POST, path, pathParams)
}
Response put(String path, Object... pathParams) {
applyPathParamsAndSendRequest(PUT, path, pathParams)
}
Response delete(String path, Object... pathParams) {
applyPathParamsAndSendRequest(DELETE, path, pathParams)
}
Response head(String path, Object... pathParams) {
applyPathParamsAndSendRequest(HEAD, path, pathParams)
}
Response patch(String path, Object... pathParams) {
applyPathParamsAndSendRequest(PATCH, path, pathParams)
}
Response options(String path, Object... pathParams) {
applyPathParamsAndSendRequest(OPTIONS, path, pathParams)
}
Response get(URI uri) {
get(notNull(uri, "URI").toString())
}
Response post(URI uri) {
post(notNull(uri, "URI").toString())
}
Response put(URI uri) {
put(notNull(uri, "URI").toString())
}
Response delete(URI uri) {
delete(notNull(uri, "URI").toString())
}
Response head(URI uri) {
head(notNull(uri, "URI").toString())
}
Response patch(URI uri) {
patch(notNull(uri, "URI").toString())
}
Response options(URI uri) {
options(notNull(uri, "URI").toString())
}
Response get(URL url) {
get(notNull(url, "URL").toString())
}
Response post(URL url) {
post(notNull(url, "URL").toString())
}
Response put(URL url) {
put(notNull(url, "URL").toString())
}
Response delete(URL url) {
delete(notNull(url, "URL").toString())
}
Response head(URL url) {
head(notNull(url, "URL").toString())
}
Response patch(URL url) {
patch(notNull(url, "URL").toString())
}
Response options(URL url) {
options(notNull(url, "URL").toString())
}
Response get() {
get("")
}
Response post() {
post("")
}
Response put() {
put("")
}
Response delete() {
delete("")
}
Response head() {
head("")
}
Response patch() {
patch("")
}
Response options() {
options("")
}
Response request(Method method) {
request(notNull(method, Method.class).name())
}
Response request(String method) {
request(method, "")
}
Response request(Method method, String path, Object... pathParams) {
return request(notNull(method, Method.class).name(), path, pathParams)
}
Response request(String method, String path, Object... pathParams) {
applyPathParamsAndSendRequest(method, path, pathParams)
}
Response request(Method method, URI uri) {
request(method, notNull(uri, URI.class).toString())
}
Response request(Method method, URL url) {
request(method, notNull(url, URL.class).toString())
}
Response request(String method, URI uri) {
request(method, notNull(uri, URI.class).toString())
}
Response request(String method, URL url) {
request(method, notNull(url, URL.class).toString())
}
Response get(String path, Map pathParamsMap) {
pathParams(pathParamsMap)
applyPathParamsAndSendRequest(GET, path)
}
Response post(String path, Map pathParamsMap) {
pathParams(pathParamsMap)
applyPathParamsAndSendRequest(POST, path)
}
Response put(String path, Map pathParamsMap) {
pathParams(pathParamsMap)
applyPathParamsAndSendRequest(PUT, path)
}
Response delete(String path, Map pathParamsMap) {
pathParams(pathParamsMap)
applyPathParamsAndSendRequest(DELETE, path)
}
Response head(String path, Map pathParamsMap) {
pathParams(pathParamsMap)
applyPathParamsAndSendRequest(HEAD, path)
}
Response patch(String path, Map pathParamsMap) {
pathParams(pathParamsMap)
applyPathParamsAndSendRequest(PATCH, path)
}
Response options(String path, Map pathParamsMap) {
pathParams(pathParamsMap)
applyPathParamsAndSendRequest(OPTIONS, path)
}
RequestSpecification params(String firstParameterName, Object firstParameterValue, Object... parameterNameValuePairs) {
notNull firstParameterName, "firstParameterName"
notNull firstParameterValue, "firstParameterValue"
return params(MapCreator.createMapFromParams(CollisionStrategy.OVERWRITE, firstParameterName, firstParameterValue, parameterNameValuePairs))
}
RequestSpecification params(Map parametersMap) {
notNull parametersMap, "parametersMap"
parameterUpdater.updateParameters(restAssuredConfig().paramConfig.requestParamsUpdateStrategy(), parametersMap, requestParameters)
return this
}
RequestSpecification param(String parameterName, Object... parameterValues) {
notNull parameterName, "parameterName"
parameterUpdater.updateZeroToManyParameters(restAssuredConfig().paramConfig.requestParamsUpdateStrategy(), requestParameters, parameterName, parameterValues)
return this
}
FilterableRequestSpecification removeParam(String parameterName) {
notNull parameterName, "parameterName"
requestParameters.remove(parameterName)
return this
}
RequestSpecification param(String parameterName, Collection<?> parameterValues) {
notNull parameterValues, "parameterValues"
return param(parameterName, parameterValues.toArray())
}
RequestSpecification queryParam(String parameterName, Collection<?> parameterValues) {
notNull parameterName, "parameterName"
notNull parameterValues, "parameterValues"
parameterUpdater.updateCollectionParameter(restAssuredConfig().getParamConfig().queryParamsUpdateStrategy(), queryParameters, parameterName, parameterValues)
return this
}
FilterableRequestSpecification removeQueryParam(String parameterName) {
notNull parameterName, "parameterName"
queryParameters.remove(parameterName)
return this
}
FilterableRequestSpecification removeHeader(String headerName) {
notNull headerName, "headerName"
def headersLeftAfterRemove = headers.findAll { !headerName.equalsIgnoreCase(it.getName()) }
this.requestHeaders = new Headers(headersLeftAfterRemove)
this
}
FilterableRequestSpecification removeCookie(String cookieName) {
notNull cookieName, "cookieName"
def cookiesLeftAfterRemove = cookies.findAll { !cookieName.equalsIgnoreCase(it.getName()) }
this.cookies = new Cookies(cookiesLeftAfterRemove)
this
}
FilterableRequestSpecification removeCookie(Cookie cookie) {
notNull cookie, "cookie"
removeCookie(cookie.getName())
this
}
FilterableRequestSpecification replaceHeader(String headerName, String newValue) {
notNull headerName, "headerName"
removeHeader(headerName)
def headerList = []
headerList.addAll(this.requestHeaders.list())
headerList.add(new Header(headerName, newValue))
this.requestHeaders = new Headers(headerList)
this
}
FilterableRequestSpecification replaceCookie(String cookieName, String value) {
notNull cookieName, "cookieName"
removeCookie(cookieName)
cookie(cookieName, value)
this
}
FilterableRequestSpecification replaceCookie(Cookie cookie) {
notNull cookie, "cookie"
removeCookie(cookie.getName())
this.cookie(cookie)
this
}
FilterableRequestSpecification replaceHeaders(Headers headers) {
notNull headers, "headers"
this.requestHeaders = new Headers(headers.asList())
this
}
FilterableRequestSpecification replaceCookies(Cookies cookies) {
notNull cookies, "cookies"
this.cookies = new Cookies(cookies.asList())
this
}
FilterableRequestSpecification removeHeaders() {
this.requestHeaders = new Headers([])
this
}
FilterableRequestSpecification removeCookies() {
this.cookies = new Cookies([])
this
}
RequestSpecification queryParams(String firstParameterName, Object firstParameterValue, Object... parameterNameValuePairs) {
notNull firstParameterName, "firstParameterName"
notNull firstParameterValue, "firstParameterValue"
return queryParams(MapCreator.createMapFromParams(CollisionStrategy.OVERWRITE, firstParameterName, firstParameterValue, parameterNameValuePairs))
}
RequestSpecification queryParams(Map parametersMap) {
notNull parametersMap, "parametersMap"
parameterUpdater.updateParameters(restAssuredConfig().paramConfig.queryParamsUpdateStrategy(), parametersMap, queryParameters)
return this
}
RequestSpecification queryParam(String parameterName, Object... parameterValues) {
notNull parameterName, "parameterName"
parameterUpdater.updateZeroToManyParameters(restAssuredConfig().paramConfig.queryParamsUpdateStrategy(), queryParameters, parameterName, parameterValues)
return this
}
RequestSpecification formParam(String parameterName, Collection<?> parameterValues) {
notNull parameterName, "parameterName"
notNull parameterValues, "parameterValues"
parameterUpdater.updateCollectionParameter(restAssuredConfig().paramConfig.formParamsUpdateStrategy(), formParameters, parameterName, parameterValues)
return this
}
FilterableRequestSpecification removeFormParam(String parameterName) {
notNull parameterName, "parameterName"
formParameters.remove(parameterName)
return this
}
RequestSpecification formParams(String firstParameterName, Object firstParameterValue, Object... parameterNameValuePairs) {
notNull firstParameterName, "firstParameterName"
notNull firstParameterValue, "firstParameterValue"
return formParams(MapCreator.createMapFromParams(CollisionStrategy.OVERWRITE, firstParameterName, firstParameterValue, parameterNameValuePairs))
}
RequestSpecification formParams(Map parametersMap) {
notNull parametersMap, "parametersMap"
parameterUpdater.updateParameters(restAssuredConfig().paramConfig.formParamsUpdateStrategy(), parametersMap, formParameters)
return this
}
RequestSpecification formParam(String parameterName, Object... additionalParameterValues) {
notNull parameterName, "parameterName"
parameterUpdater.updateZeroToManyParameters(restAssuredConfig().paramConfig.formParamsUpdateStrategy(), formParameters, parameterName, additionalParameterValues)
return this
}
RequestSpecification urlEncodingEnabled(boolean isEnabled) {
this.urlEncodingEnabled = isEnabled
return this
}
RequestSpecification pathParam(String parameterName, Object parameterValue) {
notNull parameterName, "parameterName"
notNull parameterValue, "parameterValue"
parameterUpdater.updateStandardParameter(REPLACE, namedPathParameters, parameterName, parameterValue)
return this
}
RequestSpecification pathParams(String firstParameterName, Object firstParameterValue, Object... parameterNameValuePairs) {
notNull firstParameterName, "firstParameterName"
notNull firstParameterValue, "firstParameterValue"
return pathParams(MapCreator.createMapFromParams(CollisionStrategy.OVERWRITE, firstParameterName, firstParameterValue, parameterNameValuePairs))
}
RequestSpecification pathParams(Map parameterNameValuePairs) {
notNull parameterNameValuePairs, "parameterNameValuePairs"
parameterUpdater.updateParameters(REPLACE, parameterNameValuePairs, namedPathParameters)
return this
}
FilterableRequestSpecification removePathParam(String parameterName) {
notNull parameterName, "parameterName"
removeNamedPathParam(parameterName)
removeUnnamedPathParam(parameterName)
return this
}
FilterableRequestSpecification removeNamedPathParam(String parameterName) {
notNull parameterName, "parameterName"
namedPathParameters.remove(parameterName)
this
}
FilterableRequestSpecification removeUnnamedPathParam(String parameterName) {
notNull parameterName, "parameterName"
def indexOfParamName = unnamedPathParamsTuples.findIndexOf { it.v1 == parameterName }
if (indexOfParamName > -1) {
removeUnnamedPathParamAtIndex(indexOfParamName)
}
this
}
FilterableRequestSpecification removeUnnamedPathParamByValue(String parameterValue) {
notNull parameterValue, "parameterValue"
def indexOfParamValue = unnamedPathParamsTuples.findIndexOf { it.v2 == parameterValue }
if (indexOfParamValue > -1) {
removeUnnamedPathParamAtIndex(indexOfParamValue)
}
return this
}
RequestSpecification config(RestAssuredConfig config) {
this.restAssuredConfig = config
responseSpecification?.config = config
this
}
RequestSpecification keyStore(String pathToJks, String password) {
def sslConfig = restAssuredConfig().getSSLConfig()
// Allow all host names in order to be backward compatible
restAssuredConfig = restAssuredConfig().sslConfig(sslConfig.keyStore(pathToJks, password).allowAllHostnames())
this
}
RequestSpecification keyStore(File pathToJks, String password) {
def sslConfig = restAssuredConfig().getSSLConfig()
// Allow all host names in order to be backward compatible
restAssuredConfig = restAssuredConfig().sslConfig(sslConfig.keyStore(pathToJks, password).allowAllHostnames())
this
}
RequestSpecification trustStore(String path, String password) {
def sslConfig = restAssuredConfig().getSSLConfig()
restAssuredConfig = restAssuredConfig().sslConfig(sslConfig.trustStore(path, password).allowAllHostnames())
this
}
RequestSpecification trustStore(File path, String password) {
def sslConfig = restAssuredConfig().getSSLConfig()
restAssuredConfig = restAssuredConfig().sslConfig(sslConfig.trustStore(path, password).allowAllHostnames())
this
}
RequestSpecification trustStore(KeyStore trustStore) {
def sslConfig = restAssuredConfig().getSSLConfig()
restAssuredConfig = restAssuredConfig().sslConfig(sslConfig.trustStore(trustStore))
this
}
RequestSpecification keyStore(KeyStore keyStore) {
def sslConfig = restAssuredConfig().getSSLConfig()
restAssuredConfig = restAssuredConfig().sslConfig(sslConfig.keyStore(keyStore))
this
}
RequestSpecification relaxedHTTPSValidation() {
relaxedHTTPSValidation(SSL)
}
RequestSpecification relaxedHTTPSValidation(String protocol) {
def sslConfig = restAssuredConfig().getSSLConfig()
restAssuredConfig = restAssuredConfig().sslConfig(sslConfig.relaxedHTTPSValidation(protocol))
this
}
RequestSpecification filter(Filter filter) {
notNull filter, "Filter"
filters << filter
return this
}
RequestSpecification filters(List<Filter> filters) {
notNull filters, "Filters"
this.filters.addAll(filters)
return this
}
RequestSpecification filters(Filter filter, Filter... additionalFilter) {
notNull filter, "Filter"
this.filters.add(filter)
additionalFilter?.each {
this.filters.add(it)
}
return this
}
RequestLogSpecification log() {
def blacklistedHeaders = restAssuredConfig().logConfig.blacklistedHeaders()
return new RequestLogSpecificationImpl(requestSpecification: this, logRepository: logRepository, blacklistedHeaders: blacklistedHeaders)
}
RequestSpecification and() {
return this
}
RequestSpecification request() {
return this
}
RequestSpecification with() {
return this
}
ResponseSpecification then() {
return responseSpecification
}
ResponseSpecification expect() {
return responseSpecification
}
AuthenticationSpecification auth() {
return new AuthenticationSpecificationImpl(this)
}
@Override
RequestSpecification csrf(String csrfTokenPath) {
this.restAssuredConfig = restAssuredConfig().csrfConfig(csrfConfig().csrfTokenPath(csrfTokenPath))
return this
}
@Override
RequestSpecification csrf(String csrfTokenPath, String csrfInputFieldName) {
this.restAssuredConfig = restAssuredConfig().csrfConfig(csrfConfig().csrfTokenPath(csrfTokenPath).csrfInputFieldName(csrfInputFieldName))
return this
}
@Override
RequestSpecification disableCsrf() {
addCsrfFilter = false
return noFiltersOfType(CsrfFilter.class)
}
AuthenticationSpecification authentication() {
return auth()
}
RequestSpecification port(int port) {
if (port < 1 && port != RestAssured.UNDEFINED_PORT) {
throw new IllegalArgumentException("Port must be greater than 0")
}
this.port = port
return this
}
RequestSpecification body(String body) {
notNull body, "body"
this.requestBody = body
return this
}
RequestSpecification baseUri(String baseUri) {
notNull baseUri, "Base URI"
this.baseUri = baseUri
return this
}
RequestSpecification basePath(String basePath) {
notNull basePath, "Base Path"
this.basePath = basePath
return this
}
RequestSpecification proxy(String host, int port) {
proxy(ProxySpecification.host(host).withPort(port))
}
RequestSpecification proxy(String host) {
if (UriValidator.isUri(host)) {
proxy(new URI(host))
} else {
proxy(ProxySpecification.host(host))
}
}
RequestSpecification proxy(int port) {
proxy(ProxySpecification.port(port))
}
RequestSpecification proxy(String host, int port, String scheme) {
proxy(new org.apache.http.client.utils.URIBuilder().setHost(host).setPort(port).setScheme(scheme).build())
}
RequestSpecification proxy(URI uri) {
notNull(uri, URI.class)
proxy(new ProxySpecification(uri.host, uri.port, uri.scheme))
}
RequestSpecification proxy(ProxySpecification proxySpecification) {
notNull(proxySpecification, ProxySpecification.class)
this.proxySpecification = proxySpecification
this
}
RequestSpecification body(byte[] body) {
notNull body, "body"
this.requestBody = body
return this
}
RequestSpecification body(File body) {
notNull body, "body"
this.requestBody = body
return this
}
RequestSpecification body(InputStream body) {
notNull body, "body"
this.requestBody = body
return this
}
RequestSpecification body(Object object) {
notNull object, "object"
if (!isSerializableCandidate(object)) {
return body(object.toString())
}
this.requestBody = ObjectMapping.serialize(object, requestContentType, findEncoderCharsetOrReturnDefault(requestContentType), null, objectMappingConfig(), restAssuredConfig().getEncoderConfig())
this
}
RequestSpecification body(Object object, ObjectMapper mapper) {
notNull object, "object"
notNull mapper, "Object mapper"
def ctx = new ObjectMapperSerializationContextImpl()
ctx.setObject(object)
ctx.setCharset(findEncoderCharsetOrReturnDefault(requestContentType))
ctx.setContentType(requestContentType)
this.requestBody = mapper.serialize(ctx)
this
}
RequestSpecification body(Object object, ObjectMapperType mapperType) {
notNull object, "object"
notNull mapperType, "Object mapper type"
this.requestBody = ObjectMapping.serialize(object, requestContentType, findEncoderCharsetOrReturnDefault(requestContentType), mapperType, objectMappingConfig(), restAssuredConfig().getEncoderConfig())
this
}
RequestSpecification contentType(ContentType contentType) {
notNull contentType, ContentType.class
allowContentType = true
header(CONTENT_TYPE, contentType)
}
RequestSpecification contentType(String contentType) {
notNull contentType, "Content-Type header cannot be null"
allowContentType = true
header(CONTENT_TYPE, contentType)
}
RequestSpecification noContentType() {
allowContentType = false
removeHeader(CONTENT_TYPE)
}
RequestSpecification accept(ContentType contentType) {
notNull contentType, "Accept header"
accept(contentType.getAcceptHeader())
}
RequestSpecification accept(String mediaTypes) {
notNull mediaTypes, "Accept header media range"
header(ACCEPT_HEADER_NAME, mediaTypes)
}
RequestSpecification headers(Map headers) {
notNull headers, "headers"
def headerList = []
if (this.requestHeaders.exist()) {
headerList.addAll(this.requestHeaders.list())
}
headers.each {
if (it.value instanceof List) {
it.value.each { val ->
headerList << new Header(it.key, serializeIfNeeded(val))
}
} else {
headerList << new Header(it.key, serializeIfNeeded(it.value))
}
}
headerList = removeMergedHeadersIfNeeded(headerList)
this.requestHeaders = new Headers(headerList)
return this
}
RequestSpecification headers(Headers headers) {
notNull headers, "headers"
if (headers.exist()) {
def headerList = []
if (this.requestHeaders.exist()) {
headerList.addAll(this.requestHeaders.list())
}
headerList.addAll(headers.headers.list())
headerList = removeMergedHeadersIfNeeded(headerList)
this.requestHeaders = new Headers(headerList)
}
this
}
private List removeMergedHeadersIfNeeded(List headerList) {
def headers = headerList.inject([], { acc, header ->
def headerConfig = restAssuredConfig().getHeaderConfig()
String headerName = header.getName()
if (headerConfig.shouldOverwriteHeaderWithName(headerName)) {
acc = acc.findAll { !headerName.equalsIgnoreCase(it.getName()) }
}
acc.add(header)
acc
})
headers
}
RequestSpecification header(String headerName, Object headerValue, Object... additionalHeaderValues) {
notNull headerName, "Header name"
notNull headerValue, "Header value"
def headerList = [new Header(headerName, serializeIfNeeded(headerValue))]
additionalHeaderValues?.each {
headerList << new Header(headerName, serializeIfNeeded(it))
}
return headers(new Headers(headerList))
}
RequestSpecification header(Header header) {
notNull header, "Header"
return headers(new Headers(asList(header)))
}
RequestSpecification headers(String firstHeaderName, Object firstHeaderValue, Object... headerNameValuePairs) {
return headers(MapCreator.createMapFromParams(CollisionStrategy.MERGE, firstHeaderName, firstHeaderValue, headerNameValuePairs))
}
RequestSpecification cookies(String firstCookieName, Object firstCookieValue, Object... cookieNameValuePairs) {
return cookies(MapCreator.createMapFromParams(CollisionStrategy.OVERWRITE, firstCookieName, firstCookieValue, cookieNameValuePairs))
}
RequestSpecification cookies(Map cookies) {
notNull cookies, "cookies"
def cookieList = []
if (this.cookies.exist()) {
cookieList.addAll(this.cookies.list())
}
cookies.each {
cookieList << new Cookie.Builder(it.key, it.value).build()
}
this.cookies = new Cookies(cookieList)
return this
}
RequestSpecification cookies(Cookies cookies) {
notNull cookies, "cookies"
if (cookies.exist()) {
def cookieList = []
if (this.cookies.exist()) {
cookieList.addAll(this.cookies.list())
}
cookieList.addAll(cookies.cookies.list())
this.cookies = new Cookies(cookieList)
}
this
}
RequestSpecification cookie(String cookieName, Object value, Object... additionalValues) {
notNull cookieName, "Cookie name"
def cookieList = [new Cookie.Builder(cookieName, serializeIfNeeded(value)).build()]
additionalValues?.each {
cookieList << new Cookie.Builder(cookieName, serializeIfNeeded(it)).build()
}
return cookies(new Cookies(cookieList))
}
RequestSpecification cookie(Cookie cookie) {
notNull cookie, "Cookie"
return cookies(new Cookies(asList(cookie)))
}
RequestSpecification cookie(String cookieName) {
cookie(cookieName, null)
}
RedirectSpecification redirects() {
new RedirectSpecificationImpl(this, httpClientParams)
}
RequestSpecification spec(RequestSpecification requestSpecificationToMerge) {
SpecificationMerger.merge this, requestSpecificationToMerge
return this
}
RequestSpecification specification(RequestSpecification requestSpecificationToMerge) {
return spec(requestSpecificationToMerge)
}
RequestSpecification sessionId(String sessionIdValue) {
def sessionIdName = config == null ? SessionConfig.DEFAULT_SESSION_ID_NAME : config.getSessionConfig().sessionIdName()
sessionId(sessionIdName, sessionIdValue)
}
RequestSpecification sessionId(String sessionIdName, String sessionIdValue) {
notNull(sessionIdName, "Session id name")
notNull(sessionIdValue, "Session id value")
if (cookies.hasCookieWithName(sessionIdName)) {
def allOtherCookies = cookies.findAll { !it.getName().equalsIgnoreCase(sessionIdName) }
allOtherCookies.add(new Cookie.Builder(sessionIdName, sessionIdValue).build())
this.cookies = new Cookies(allOtherCookies)
} else {
cookie(sessionIdName, sessionIdValue)
}
this
}
RequestSpecification multiPart(MultiPartSpecification multiPartSpec) {
notNull multiPartSpec, "Multi-part specification"
def mimeType = multiPartSpec.mimeType
def content
if (multiPartSpec.content instanceof File || multiPartSpec.content instanceof InputStream || multiPartSpec.content instanceof byte[]) {
content = multiPartSpec.content
} else {
// Objects ought to be serialized
if (mimeType == null) {
mimeType = ANY.matches(requestContentType) ? JSON.toString() : requestContentType
}
content = serializeIfNeeded(multiPartSpec.content, mimeType)
}
final String controlName
if (multiPartSpec instanceof MultiPartSpecificationImpl && !multiPartSpec.isControlNameSpecifiedExplicitly()) {
// We use the default control name if it was not explicitly specified in the multi-part spec
controlName = restAssuredConfig().getMultiPartConfig().defaultControlName()
} else {
controlName = multiPartSpec.controlName
}
final String fileName
if (multiPartSpec instanceof MultiPartSpecificationImpl && !multiPartSpec.isFileNameSpecifiedExplicitly()) {
// We use the default file name if it was not explicitly specified in the multi-part spec
fileName = restAssuredConfig().getMultiPartConfig().defaultFileName()
} else {
fileName = multiPartSpec.fileName
}
def headers = multiPartSpec.headers