Best Webtau code snippet using org.testingisdocumenting.webtau.http.HttpHeader.HttpHeader
Source:Http.java
...76 private final ThreadLocal<HttpValidationResult> lastValidationResult = new ThreadLocal<>();77 public final HttpApplicationMime application = new HttpApplicationMime();78 public final HttpTextMime text = new HttpTextMime();79 public boolean ping(String url) {80 return ping(url, HttpQueryParams.EMPTY, HttpHeader.EMPTY);81 }82 public boolean ping(String url, HttpQueryParams queryParams) {83 return ping(url, queryParams, HttpHeader.EMPTY);84 }85 public boolean ping(String url, HttpHeader header) {86 return ping(url, HttpQueryParams.EMPTY, header);87 }88 public boolean ping(String url, HttpQueryParams queryParams, HttpHeader header) {89 String fullUrl = WebTauHttpConfigurations.fullUrl(queryParams.attachToUrl(url));90 WebTauStep step = WebTauStep.createStep(91 tokenizedMessage(action("pinging"), urlValue(fullUrl)),92 () -> tokenizedMessage(action("pinged"), urlValue(fullUrl)),93 () -> HttpValidationHandlers.withDisabledHandlers(() -> {94 HttpOperationIdProviders.withDisabledProviders(() -> {95 http.get(url, header);96 return null;97 });98 return null;99 })100 );101 try {102 step.execute(StepReportOptions.REPORT_ALL);103 } catch (Throwable e) {104 return false;105 }106 return true;107 }108 public String concatUrl(String baseUrl, String relativeUrl) {109 return UrlUtils.concat(baseUrl, relativeUrl);110 }111 public HttpRequestBody json(String firstKey, Object firstValue, Object... rest) {112 return application.json(firstKey, firstValue, rest);113 }114 public HttpRequestBody json(String json) {115 return application.json(json);116 }117 public <E> E get(String url,118 HttpQueryParams queryParams,119 HttpHeader header,120 HttpResponseValidatorWithReturn validator) {121 return executeAndValidateHttpCall("GET", queryParams.attachToUrl(url),122 this::getToFullUrl,123 header, null, validator);124 }125 public <E> E get(String url, Map<CharSequence, ?> queryParams, HttpHeader header, HttpResponseValidatorWithReturn validator) {126 return get(url, new HttpQueryParams(queryParams), header, validator);127 }128 public void get(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidator validator) {129 get(url, queryParams, header, new HttpResponseValidatorIgnoringReturn(validator));130 }131 public void get(String url, Map<CharSequence, ?> queryParams, HttpHeader header, HttpResponseValidator validator) {132 get(url, queryParams, header, new HttpResponseValidatorIgnoringReturn(validator));133 }134 public void get(String url, HttpQueryParams queryParams, HttpHeader header) {135 get(url, queryParams, header, EMPTY_RESPONSE_VALIDATOR);136 }137 public void get(String url, Map<CharSequence, ?> queryParams, HttpHeader header) {138 get(url, queryParams, header, EMPTY_RESPONSE_VALIDATOR);139 }140 public <E> E get(String url, Map<CharSequence, ?> queryParams, HttpResponseValidatorWithReturn validator) {141 return get(url, queryParams, HttpHeader.EMPTY, validator);142 }143 public <E> E get(String url, HttpQueryParams queryParams, HttpResponseValidatorWithReturn validator) {144 return get(url, queryParams, HttpHeader.EMPTY, validator);145 }146 public void get(String url, Map<CharSequence, ?> queryParams, HttpResponseValidator validator) {147 get(url, queryParams, HttpHeader.EMPTY, new HttpResponseValidatorIgnoringReturn(validator));148 }149 public void get(String url, HttpQueryParams queryParams, HttpResponseValidator validator) {150 get(url, queryParams, HttpHeader.EMPTY, new HttpResponseValidatorIgnoringReturn(validator));151 }152 public void get(String url, Map<CharSequence, ?> queryParams) {153 get(url, queryParams, HttpHeader.EMPTY, EMPTY_RESPONSE_VALIDATOR);154 }155 public void get(String url, HttpQueryParams queryParams) {156 get(url, queryParams, HttpHeader.EMPTY, EMPTY_RESPONSE_VALIDATOR);157 }158 public <E> E get(String url, HttpHeader header, HttpResponseValidatorWithReturn validator) {159 return get(url, HttpQueryParams.EMPTY, header, validator);160 }161 public void get(String url, HttpHeader header, HttpResponseValidator validator) {162 get(url, HttpQueryParams.EMPTY, header, new HttpResponseValidatorIgnoringReturn(validator));163 }164 public void get(String url, HttpHeader header) {165 get(url, HttpQueryParams.EMPTY, header, EMPTY_RESPONSE_VALIDATOR);166 }167 public <E> E get(String url, HttpResponseValidatorWithReturn validator) {168 return get(url, HttpQueryParams.EMPTY, HttpHeader.EMPTY, validator);169 }170 public void get(String url, HttpResponseValidator validator) {171 get(url, HttpQueryParams.EMPTY, HttpHeader.EMPTY, new HttpResponseValidatorIgnoringReturn(validator));172 }173 public void get(String url) {174 get(url, HttpQueryParams.EMPTY, HttpHeader.EMPTY, EMPTY_RESPONSE_VALIDATOR);175 }176 public <E> E patch(String url, HttpQueryParams queryParams, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {177 return executeAndValidateHttpCall(178 "PATCH",179 queryParams.attachToUrl(url),180 (fullUrl, fullHeader) -> patchToFullUrl(fullUrl, fullHeader, requestBody),181 header,182 requestBody,183 validator);184 }185 public <E> E patch(String url, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {186 return patch(url, HttpQueryParams.EMPTY, header, requestBody, validator);187 }188 public void patch(String url, HttpQueryParams queryParams, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidator validator) {189 patch(url, queryParams, header, requestBody, new HttpResponseValidatorIgnoringReturn(validator));190 }191 public void patch(String url, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidator validator) {192 patch(url, header, requestBody, new HttpResponseValidatorIgnoringReturn(validator));193 }194 public <E> E patch(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidatorWithReturn validator) {195 return patch(url, queryParams, header, EmptyRequestBody.INSTANCE, validator);196 }197 public <E> E patch(String url, HttpHeader header, HttpResponseValidatorWithReturn validator) {198 return patch(url, header, EmptyRequestBody.INSTANCE, validator);199 }200 public void patch(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidator validator) {201 patch(url, queryParams, header, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));202 }203 public void patch(String url, HttpHeader header, HttpResponseValidator validator) {204 patch(url, header, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));205 }206 public <E> E patch(String url, HttpQueryParams queryParams, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {207 return patch(url, queryParams, HttpHeader.EMPTY, requestBody, validator);208 }209 public <E> E patch(String url, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {210 return patch(url, HttpHeader.EMPTY, requestBody, validator);211 }212 public void patch(String url, HttpQueryParams queryParams, HttpRequestBody requestBody, HttpResponseValidator validator) {213 patch(url, queryParams, requestBody, new HttpResponseValidatorIgnoringReturn(validator));214 }215 public void patch(String url, HttpRequestBody requestBody, HttpResponseValidator validator) {216 patch(url, requestBody, new HttpResponseValidatorIgnoringReturn(validator));217 }218 public <E> E patch(String url, HttpQueryParams queryParams, HttpHeader header, Map<String, ?> requestBody,219 HttpResponseValidatorWithReturn validator) {220 return patch(url, queryParams, header, new JsonRequestBody(requestBody), validator);221 }222 public <E> E patch(String url, HttpQueryParams queryParams, HttpHeader header, Collection<?> requestBody,223 HttpResponseValidatorWithReturn validator) {224 return patch(url, queryParams, header, new JsonRequestBody(requestBody), validator);225 }226 public <E> E patch(String url, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {227 return patch(url, header, new JsonRequestBody(requestBody), validator);228 }229 public <E> E patch(String url, HttpHeader header, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {230 return patch(url, header, new JsonRequestBody(requestBody), validator);231 }232 public void patch(String url, HttpQueryParams queryParams, HttpHeader header, Map<String, ?> requestBody,233 HttpResponseValidator validator) {234 patch(url, queryParams, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));235 }236 public void patch(String url, HttpQueryParams queryParams, HttpHeader header, Collection<?> requestBody,237 HttpResponseValidator validator) {238 patch(url, queryParams, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));239 }240 public void patch(String url, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidator validator) {241 patch(url, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));242 }243 public void patch(String url, HttpHeader header, Collection<?> requestBody, HttpResponseValidator validator) {244 patch(url, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));245 }246 public <E> E patch(String url, HttpQueryParams queryParams, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {247 return patch(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);248 }249 public <E> E patch(String url, HttpQueryParams queryParams, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {250 return patch(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);251 }252 public <E> E patch(String url, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {253 return patch(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);254 }255 public <E> E patch(String url, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {256 return patch(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);257 }258 public void patch(String url, HttpQueryParams queryParams, Map<String, ?> requestBody, HttpResponseValidator validator) {259 patch(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));260 }261 public void patch(String url, HttpQueryParams queryParams, Collection<?> requestBody, HttpResponseValidator validator) {262 patch(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));263 }264 public void patch(String url, Map<String, ?> requestBody, HttpResponseValidator validator) {265 patch(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));266 }267 public void patch(String url, Collection<?> requestBody, HttpResponseValidator validator) {268 patch(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));269 }270 public void patch(String url, Map<String, ?> requestBody) {271 patch(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);272 }273 public void patch(String url, Collection<?> requestBody) {274 patch(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);275 }276 public <E> E patch(String url, HttpQueryParams queryParams, HttpResponseValidatorWithReturn validator) {277 return patch(url, queryParams, EmptyRequestBody.INSTANCE, validator);278 }279 public <E> E patch(String url, HttpResponseValidatorWithReturn validator) {280 return patch(url, EmptyRequestBody.INSTANCE, validator);281 }282 public void patch(String url, HttpQueryParams queryParams, HttpResponseValidator validator) {283 patch(url, queryParams, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));284 }285 public void patch(String url, HttpResponseValidator validator) {286 patch(url, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));287 }288 public void patch(String url, HttpQueryParams queryParams, HttpHeader header) {289 patch(url, queryParams, header, EMPTY_RESPONSE_VALIDATOR);290 }291 public void patch(String url, HttpHeader header) {292 patch(url, header, EMPTY_RESPONSE_VALIDATOR);293 }294 public void patch(String url, HttpQueryParams queryParams) {295 patch(url, queryParams, EMPTY_RESPONSE_VALIDATOR);296 }297 public void patch(String url) {298 patch(url, EMPTY_RESPONSE_VALIDATOR);299 }300 public void patch(String url, HttpQueryParams queryParams, HttpRequestBody requestBody) {301 patch(url, queryParams, requestBody, EMPTY_RESPONSE_VALIDATOR);302 }303 public void patch(String url, HttpRequestBody requestBody) {304 patch(url, requestBody, EMPTY_RESPONSE_VALIDATOR);305 }306 public <E> E post(String url, HttpQueryParams queryParams, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {307 return executeAndValidateHttpCall("POST", queryParams.attachToUrl(url),308 (fullUrl, fullHeader) -> postToFullUrl(fullUrl, fullHeader, requestBody),309 header,310 requestBody,311 validator);312 }313 public <E> E post(String url, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {314 return post(url, HttpQueryParams.EMPTY, header, requestBody, validator);315 }316 public void post(String url, HttpQueryParams queryParams, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidator validator) {317 post(url, queryParams, header, requestBody, new HttpResponseValidatorIgnoringReturn(validator));318 }319 public void post(String url, HttpQueryParams queryParams, HttpHeader header, HttpRequestBody requestBody) {320 post(url, queryParams, header, requestBody, EMPTY_RESPONSE_VALIDATOR);321 }322 public void post(String url, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidator validator) {323 post(url, header, requestBody, new HttpResponseValidatorIgnoringReturn(validator));324 }325 public void post(String url, HttpHeader header, HttpRequestBody requestBody) {326 post(url, header, requestBody, EMPTY_RESPONSE_VALIDATOR);327 }328 public <E> E post(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidatorWithReturn validator) {329 return post(url, queryParams, header, EmptyRequestBody.INSTANCE, validator);330 }331 public <E> E post(String url, HttpHeader header, HttpResponseValidatorWithReturn validator) {332 return post(url, header, EmptyRequestBody.INSTANCE, validator);333 }334 public void post(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidator validator) {335 post(url, queryParams, header, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));336 }337 public void post(String url, HttpHeader header, HttpResponseValidator validator) {338 post(url, header, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));339 }340 public <E> E post(String url, HttpQueryParams queryParams, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {341 return post(url, queryParams, HttpHeader.EMPTY, requestBody, validator);342 }343 public <E> E post(String url, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {344 return post(url, HttpHeader.EMPTY, requestBody, validator);345 }346 public void post(String url, HttpQueryParams queryParams, HttpRequestBody requestBody, HttpResponseValidator validator) {347 post(url, queryParams, requestBody, new HttpResponseValidatorIgnoringReturn(validator));348 }349 public void post(String url, HttpRequestBody requestBody, HttpResponseValidator validator) {350 post(url, requestBody, new HttpResponseValidatorIgnoringReturn(validator));351 }352 public <E> E post(String url, HttpQueryParams queryParams, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {353 return post(url, queryParams, header, new JsonRequestBody(requestBody), validator);354 }355 public <E> E post(String url, HttpQueryParams queryParams, HttpHeader header, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {356 return post(url, queryParams, header, new JsonRequestBody(requestBody), validator);357 }358 public <E> E post(String url, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {359 return post(url, header, new JsonRequestBody(requestBody), validator);360 }361 public <E> E post(String url, HttpHeader header, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {362 return post(url, header, new JsonRequestBody(requestBody), validator);363 }364 public void post(String url, HttpQueryParams queryParams, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidator validator) {365 post(url, queryParams, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));366 }367 public void post(String url, HttpQueryParams queryParams, HttpHeader header, Collection<?> requestBody, HttpResponseValidator validator) {368 post(url, queryParams, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));369 }370 public void post(String url, HttpQueryParams queryParams, HttpHeader header, Map<String, ?> requestBody) {371 post(url, queryParams, header, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);372 }373 public void post(String url, HttpQueryParams queryParams, HttpHeader header, Collection<?> requestBody) {374 post(url, queryParams, header, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);375 }376 public void post(String url, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidator validator) {377 post(url, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));378 }379 public void post(String url, HttpHeader header, Collection<?> requestBody, HttpResponseValidator validator) {380 post(url, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));381 }382 public void post(String url, HttpHeader header, Map<String, ?> requestBody) {383 post(url, header, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);384 }385 public void post(String url, HttpHeader header, Collection<?> requestBody) {386 post(url, header, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);387 }388 public <E> E post(String url, HttpQueryParams queryParams, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {389 return post(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);390 }391 public <E> E post(String url, HttpQueryParams queryParams, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {392 return post(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);393 }394 public <E> E post(String url, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {395 return post(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);396 }397 public <E> E post(String url, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {398 return post(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);399 }400 public void post(String url, HttpQueryParams queryParams, Map<String, ?> requestBody, HttpResponseValidator validator) {401 post(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));402 }403 public void post(String url, HttpQueryParams queryParams, Collection<?> requestBody, HttpResponseValidator validator) {404 post(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));405 }406 public void post(String url, HttpQueryParams queryParams, Map<String, ?> requestBody) {407 post(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);408 }409 public void post(String url, HttpQueryParams queryParams, Collection<?> requestBody) {410 post(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);411 }412 public void post(String url, Map<String, ?> requestBody, HttpResponseValidator validator) {413 post(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));414 }415 public void post(String url, Collection<?> requestBody, HttpResponseValidator validator) {416 post(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));417 }418 public void post(String url, Map<String, ?> requestBody) {419 post(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);420 }421 public void post(String url, Collection<?> requestBody) {422 post(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);423 }424 public <E> E post(String url, HttpQueryParams queryParams, HttpResponseValidatorWithReturn validator) {425 return post(url, queryParams, EmptyRequestBody.INSTANCE, validator);426 }427 public <E> E post(String url, HttpResponseValidatorWithReturn validator) {428 return post(url, EmptyRequestBody.INSTANCE, validator);429 }430 public void post(String url, HttpQueryParams queryParams, HttpResponseValidator validator) {431 post(url, queryParams, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));432 }433 public void post(String url, HttpResponseValidator validator) {434 post(url, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));435 }436 public void post(String url, HttpQueryParams queryParams, HttpHeader header) {437 post(url, queryParams, header, EMPTY_RESPONSE_VALIDATOR);438 }439 public void post(String url, HttpHeader header) {440 post(url, header, EMPTY_RESPONSE_VALIDATOR);441 }442 public void post(String url, HttpQueryParams queryParams) {443 post(url, queryParams, EMPTY_RESPONSE_VALIDATOR);444 }445 public void post(String url) {446 post(url, EMPTY_RESPONSE_VALIDATOR);447 }448 public void post(String url, HttpQueryParams queryParams, HttpRequestBody requestBody) {449 post(url, queryParams, requestBody, EMPTY_RESPONSE_VALIDATOR);450 }451 public void post(String url, HttpRequestBody requestBody) {452 post(url, requestBody, EMPTY_RESPONSE_VALIDATOR);453 }454 public <E> E put(String url, HttpQueryParams queryParams, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {455 return executeAndValidateHttpCall("PUT", queryParams.attachToUrl(url),456 (fullUrl, fullHeader) -> putToFullUrl(fullUrl, fullHeader, requestBody),457 header,458 requestBody,459 validator);460 }461 public void put(String url, HttpQueryParams queryParams, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidator validator) {462 put(url, queryParams, header, requestBody, new HttpResponseValidatorIgnoringReturn(validator));463 }464 public void put(String url, HttpQueryParams queryParams, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidator validator) {465 put(url, queryParams, header, requestBody, new HttpResponseValidatorIgnoringReturn(validator));466 }467 public void put(String url, HttpQueryParams queryParams, HttpHeader header, Collection<?> requestBody, HttpResponseValidator validator) {468 put(url, queryParams, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));469 }470 public void put(String url, HttpQueryParams queryParams, HttpHeader header, HttpRequestBody requestBody) {471 put(url, queryParams, header, requestBody, EMPTY_RESPONSE_VALIDATOR);472 }473 public <E> E put(String url, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {474 return put(url, HttpQueryParams.EMPTY, header, requestBody, validator);475 }476 public void put(String url, HttpHeader header, HttpRequestBody requestBody, HttpResponseValidator validator) {477 put(url, header, requestBody, new HttpResponseValidatorIgnoringReturn(validator));478 }479 public void put(String url, HttpHeader header, HttpRequestBody requestBody) {480 put(url, header, requestBody, EMPTY_RESPONSE_VALIDATOR);481 }482 public <E> E put(String url, HttpQueryParams queryParams, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {483 return put(url, queryParams, HttpHeader.EMPTY, requestBody, validator);484 }485 public <E> E put(String url, HttpQueryParams queryParams, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {486 return put(url, queryParams, HttpHeader.EMPTY, requestBody, validator);487 }488 public <E> E put(String url, HttpQueryParams queryParams, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {489 return put(url, queryParams, HttpHeader.EMPTY, requestBody, validator);490 }491 public void put(String url, HttpQueryParams queryParams, HttpRequestBody requestBody, HttpResponseValidator validator) {492 put(url, queryParams, requestBody, new HttpResponseValidatorIgnoringReturn(validator));493 }494 public void put(String url, HttpQueryParams queryParams, Map<String, ?> requestBody, HttpResponseValidator validator) {495 put(url, queryParams, requestBody, new HttpResponseValidatorIgnoringReturn(validator));496 }497 public void put(String url, HttpQueryParams queryParams, Collection<?> requestBody, HttpResponseValidator validator) {498 put(url, queryParams, requestBody, new HttpResponseValidatorIgnoringReturn(validator));499 }500 public void put(String url, HttpQueryParams queryParams, HttpRequestBody requestBody) {501 put(url, queryParams, requestBody, EMPTY_RESPONSE_VALIDATOR);502 }503 public <E> E put(String url, HttpRequestBody requestBody, HttpResponseValidatorWithReturn validator) {504 return put(url, HttpHeader.EMPTY, requestBody, validator);505 }506 public void put(String url, HttpRequestBody requestBody, HttpResponseValidator validator) {507 put(url, requestBody, new HttpResponseValidatorIgnoringReturn(validator));508 }509 public void put(String url, HttpRequestBody requestBody) {510 put(url, requestBody, EMPTY_RESPONSE_VALIDATOR);511 }512 public <E> E put(String url, HttpQueryParams queryParams, HttpResponseValidatorWithReturn validator) {513 return put(url, queryParams, HttpHeader.EMPTY, EmptyRequestBody.INSTANCE, validator);514 }515 public void put(String url, HttpQueryParams queryParams, HttpResponseValidator validator) {516 put(url, queryParams, HttpHeader.EMPTY, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));517 }518 public void put(String url, HttpQueryParams queryParams) {519 put(url, queryParams, HttpHeader.EMPTY, EmptyRequestBody.INSTANCE, EMPTY_RESPONSE_VALIDATOR);520 }521 public <E> E put(String url, HttpResponseValidatorWithReturn validator) {522 return put(url, HttpHeader.EMPTY, EmptyRequestBody.INSTANCE, validator);523 }524 public void put(String url, HttpResponseValidator validator) {525 put(url, HttpHeader.EMPTY, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));526 }527 public void put(String url) {528 put(url, HttpHeader.EMPTY, EmptyRequestBody.INSTANCE, EMPTY_RESPONSE_VALIDATOR);529 }530 public <E> E put(String url, HttpQueryParams queryParams, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {531 return put(url, queryParams, header, new JsonRequestBody(requestBody), validator);532 }533 public <E> E put(String url, HttpQueryParams queryParams, HttpHeader header, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {534 return put(url, queryParams, header, new JsonRequestBody(requestBody), validator);535 }536 public <E> E put(String url, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {537 return put(url, header, new JsonRequestBody(requestBody), validator);538 }539 public <E> E put(String url, HttpHeader header, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {540 return put(url, header, new JsonRequestBody(requestBody), validator);541 }542 public void put(String url, HttpHeader header, Map<String, ?> requestBody, HttpResponseValidator validator) {543 put(url, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));544 }545 public void put(String url, HttpHeader header, Collection<?> requestBody, HttpResponseValidator validator) {546 put(url, header, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));547 }548 public void put(String url, HttpHeader header, Map<String, ?> requestBody) {549 put(url, header, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);550 }551 public void put(String url, HttpHeader header, Collection<?> requestBody) {552 put(url, header, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);553 }554 public void put(String url, HttpQueryParams queryParams, HttpHeader header, Map<String, ?> requestBody) {555 put(url, queryParams, header, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);556 }557 public void put(String url, HttpQueryParams queryParams, HttpHeader header, Collection<?> requestBody) {558 put(url, queryParams, header, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);559 }560 public <E> E put(String url, Map<String, ?> requestBody, HttpResponseValidatorWithReturn validator) {561 return put(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);562 }563 public <E> E put(String url, Collection<?> requestBody, HttpResponseValidatorWithReturn validator) {564 return put(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), validator);565 }566 public void put(String url, Map<String, ?> requestBody, HttpResponseValidator validator) {567 put(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));568 }569 public void put(String url, Collection<?> requestBody, HttpResponseValidator validator) {570 put(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));571 }572 public void put(String url, Map<String, ?> requestBody) {573 put(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);574 }575 public void put(String url, Collection<?> requestBody) {576 put(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);577 }578 public void put(String url, HttpQueryParams queryParams, Map<String, ?> requestBody) {579 put(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);580 }581 public void put(String url, HttpQueryParams queryParams, Collection<?> requestBody) {582 put(url, queryParams, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);583 }584 public <E> E put(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidatorWithReturn validator) {585 return put(url, queryParams, header, EmptyRequestBody.INSTANCE, validator);586 }587 public void put(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidator validator) {588 put(url, queryParams, header, new HttpResponseValidatorIgnoringReturn(validator));589 }590 public void put(String url, HttpQueryParams queryParams, HttpHeader header) {591 put(url, queryParams, header, EMPTY_RESPONSE_VALIDATOR);592 }593 public <E> E put(String url, HttpHeader header, HttpResponseValidatorWithReturn validator) {594 return put(url, header, EmptyRequestBody.INSTANCE, validator);595 }596 public void put(String url, HttpHeader header, HttpResponseValidator validator) {597 put(url, header, EmptyRequestBody.INSTANCE, new HttpResponseValidatorIgnoringReturn(validator));598 }599 public void put(String url, HttpHeader header) {600 put(url, header, EmptyRequestBody.INSTANCE, EMPTY_RESPONSE_VALIDATOR);601 }602 public <E> E delete(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidatorWithReturn validator) {603 return executeAndValidateHttpCall("DELETE", queryParams.attachToUrl(url),604 this::deleteToFullUrl,605 header,606 null,607 validator);608 }609 public void delete(String url, HttpQueryParams queryParams, HttpHeader header, HttpResponseValidator validator) {610 delete(url, queryParams, header, new HttpResponseValidatorIgnoringReturn(validator));611 }612 public <E> E delete(String url, HttpHeader header, HttpResponseValidatorWithReturn validator) {613 return delete(url, HttpQueryParams.EMPTY, header, validator);614 }615 public void delete(String url, HttpHeader header, HttpResponseValidator validator) {616 delete(url, HttpQueryParams.EMPTY, header, new HttpResponseValidatorIgnoringReturn(validator));617 }618 public <E> E delete(String url, HttpQueryParams queryParams, HttpResponseValidatorWithReturn validator) {619 return delete(url, queryParams, HttpHeader.EMPTY, validator);620 }621 public void delete(String url, HttpQueryParams queryParams, HttpResponseValidator validator) {622 delete(url, queryParams, HttpHeader.EMPTY, new HttpResponseValidatorIgnoringReturn(validator));623 }624 public <E> E delete(String url, HttpResponseValidatorWithReturn validator) {625 return delete(url, HttpQueryParams.EMPTY, HttpHeader.EMPTY, validator);626 }627 public void delete(String url, HttpResponseValidator validator) {628 delete(url, HttpQueryParams.EMPTY, HttpHeader.EMPTY, new HttpResponseValidatorIgnoringReturn(validator));629 }630 public void delete(String url, HttpQueryParams queryParams, HttpHeader header) {631 delete(url, queryParams, header, EMPTY_RESPONSE_VALIDATOR);632 }633 public void delete(String url, HttpHeader header) {634 delete(url, HttpQueryParams.EMPTY, header, EMPTY_RESPONSE_VALIDATOR);635 }636 public void delete(String url, HttpQueryParams queryParams) {637 delete(url, queryParams, HttpHeader.EMPTY, EMPTY_RESPONSE_VALIDATOR);638 }639 public void delete(String url) {640 delete(url, HttpQueryParams.EMPTY, HttpHeader.EMPTY, EMPTY_RESPONSE_VALIDATOR);641 }642 public HttpHeader header(CharSequence firstKey, CharSequence firstValue, CharSequence... restKv) {643 return new HttpHeader().with(firstKey, firstValue, restKv);644 }645 public HttpHeader header(Map<CharSequence, CharSequence> properties) {646 return new HttpHeader().with(properties);647 }648 public HttpQueryParams query(Map<?, ?> params) {649 return new HttpQueryParams(params);650 }651 public HttpQueryParams query(CharSequence firstKey, Object firstValue, Object... restKv) {652 return new HttpQueryParams(CollectionUtils.aMapOf(firstKey, firstValue, restKv));653 }654 public HttpRequestBody body(String mimeType, String content) {655 return TextRequestBody.withType(mimeType, content);656 }657 public HttpRequestBody body(String mimeType, byte[] content) {658 return BinaryRequestBody.withType(mimeType, content);659 }660 public MultiPartFormData formData(MultiPartFormField... fields) {661 MultiPartFormData formData = new MultiPartFormData();662 Arrays.stream(fields).forEach(formData::addField);663 return formData;664 }665 public MultiPartFormData formData(Map<String, ?> fields) {666 return new MultiPartFormData(fields);667 }668 public MultiPartFormField formField(byte[] fileContent) {669 return formField("file", fileContent, null);670 }671 public MultiPartFormField formField(String fieldName, byte[] fileContent) {672 return formField(fieldName, fileContent, null);673 }674 public MultiPartFormField formField(String fieldName, Path file) {675 return formField(fieldName, file, file.getFileName().toString());676 }677 public MultiPartFormField formField(String fieldName, Path file, String fileName) {678 return MultiPartFormField.fileFormField(fieldName, file, fileName);679 }680 public MultiPartFormField formField(String fieldName, byte[] fileContent, String fileName) {681 return MultiPartFormField.binaryFormField(fieldName, fileContent, fileName);682 }683 public MultiPartFormField formField(String fieldName, String textContent, String fileName) {684 return MultiPartFormField.textFormField(fieldName, textContent, fileName);685 }686 public MultiPartFormField formField(String fieldName, String textContent) {687 return formField(fieldName, textContent, null);688 }689 public MultiPartFile formFile(String fileName, byte[] fileContent) {690 return new MultiPartFile(fileName, fileContent);691 }692 public MultiPartFile formFile(String fileName, Path file) {693 return new MultiPartFile(fileName, file);694 }695 public HttpValidationResult getLastValidationResult() {696 return lastValidationResult.get();697 }698 public HttpResponse getToFullUrl(String fullUrl, HttpHeader requestHeader) {699 return request("GET", fullUrl, requestHeader, EmptyRequestBody.INSTANCE);700 }701 public HttpResponse deleteToFullUrl(String fullUrl, HttpHeader requestHeader) {702 return request("DELETE", fullUrl, requestHeader, EmptyRequestBody.INSTANCE);703 }704 public HttpResponse patchToFullUrl(String fullUrl, HttpHeader requestHeader, HttpRequestBody requestBody) {705 return request("PATCH", fullUrl, requestHeader, requestBody);706 }707 public HttpResponse postToFullUrl(String fullUrl, HttpHeader requestHeader, HttpRequestBody requestBody) {708 return request("POST", fullUrl, requestHeader, requestBody);709 }710 public HttpResponse putToFullUrl(String fullUrl, HttpHeader requestHeader, HttpRequestBody requestBody) {711 return request("PUT", fullUrl, requestHeader, requestBody);712 }713 private <R> R executeAndValidateHttpCall(String requestMethod,714 String url,715 HttpCall httpCall,716 HttpHeader requestHeader,717 HttpRequestBody requestBody,718 HttpResponseValidatorWithReturn validator) {719 String fullUrl = WebTauHttpConfigurations.fullUrl(url);720 HttpHeader fullHeader = WebTauHttpConfigurations.fullHeader(fullUrl, url, requestHeader);721 HttpValidationResult validationResult = new HttpValidationResult(Persona.getCurrentPersona().getId(),722 requestMethod, url, fullUrl, fullHeader, requestBody);723 WebTauStep step = createHttpStep(validationResult, httpCall, validator);724 step.setInput(new HttpStepInput(validationResult));725 step.setOutputSupplier(() -> validationResult);726 try {727 return step.execute(StepReportOptions.REPORT_ALL);728 } finally {729 lastValidationResult.set(validationResult);730 }731 }732 private <R> WebTauStep createHttpStep(HttpValidationResult validationResult,733 HttpCall httpCall,734 HttpResponseValidatorWithReturn validator) {735 Supplier<Object> httpCallSupplier = () -> {736 HttpResponse response = null;737 try {738 BeforeFirstHttpCallListenerTrigger.trigger();739 HttpListeners.beforeHttpCall(validationResult.getRequestMethod(),740 validationResult.getUrl(), validationResult.getFullUrl(),741 validationResult.getRequestHeader(), validationResult.getRequestBody());742 long startTime = Time.currentTimeMillis();743 validationResult.setStartTime(startTime);744 response = httpCall.execute(validationResult.getFullUrl(),745 validationResult.getRequestHeader());746 response = followRedirects(validationResult.getRequestMethod(),747 httpCall, validationResult.getRequestHeader(), response);748 validationResult.calcElapsedTimeIfNotCalculated();749 validationResult.setResponse(response);750 751 validationResult.setOperationId(HttpOperationIdProviders.operationId(752 validationResult.getRequestMethod(),753 validationResult.getUrl(),754 validationResult.getFullUrl(),755 validationResult.getRequestHeader(),756 validationResult.getRequestBody()));757 R validationBlockReturnedValue = validateAndRecord(validationResult, validator);758 if (validationResult.hasMismatches()) {759 throw new AssertionError("\n" + validationResult.renderMismatches());760 }761 return validationBlockReturnedValue;762 } catch (AssertionError e) {763 throw e;764 } catch (Throwable e) {765 validationResult.setErrorMessage(StackTraceUtils.fullCauseMessage(e));766 throw new HttpException("error during http." + validationResult.getRequestMethod().toLowerCase() + "(" +767 validationResult.getFullUrl() + "): " + StackTraceUtils.fullCauseMessage(e), e);768 } finally {769 validationResult.calcElapsedTimeIfNotCalculated();770 HttpListeners.afterHttpCall(validationResult.getRequestMethod(),771 validationResult.getUrl(), validationResult.getFullUrl(),772 validationResult.getRequestHeader(), validationResult.getRequestBody(),773 response);774 }775 };776 return WebTauStep.createStep(777 tokenizedMessage(action("executing HTTP " + validationResult.getRequestMethod()), urlValue(validationResult.getFullUrl())),778 () -> tokenizedMessage(action("executed HTTP " + validationResult.getRequestMethod()), urlValue(validationResult.getFullUrl())),779 httpCallSupplier);780 }781 private HttpResponse followRedirects(String requestMethod, HttpCall httpCall, HttpHeader fullRequestHeader, HttpResponse response) {782 int retryCount = 0;783 while (response.isRedirect() && getCfg().shouldFollowRedirects() && retryCount++ < getCfg().maxRedirects()) {784 WebTauStep httpStep = createRedirectStep(requestMethod, response.locationHeader(), httpCall, fullRequestHeader);785 response = httpStep.execute(StepReportOptions.REPORT_ALL);786 }787 return response;788 }789 private WebTauStep createRedirectStep(String requestMethod, String fullUrl, HttpCall httpCall,790 HttpHeader fullRequestHeader) {791 Supplier<Object> httpCallSupplier = () -> httpCall.execute(fullUrl, fullRequestHeader);792 return WebTauStep.createStep(tokenizedMessage(action("executing HTTP redirect to " + requestMethod), urlValue(fullUrl)),793 () -> tokenizedMessage(action("executed HTTP redirect to " + requestMethod), urlValue(fullUrl)),794 httpCallSupplier);795 }796 @SuppressWarnings("unchecked")797 private <R> R validateAndRecord(HttpValidationResult validationResult,798 HttpResponseValidatorWithReturn validator) {799 HeaderDataNode header = new HeaderDataNode(validationResult.getResponse());800 BodyDataNode body = new BodyDataNode(validationResult.getResponse(),801 createBodyDataNodeAndMarkResponseInvalidWhenParsingError(validationResult));802 validationResult.setResponseHeaderNode(header);803 validationResult.setResponseBodyNode(body);804 ExpectationHandler recordAndThrowHandler = new ExpectationHandler() {805 @Override806 public Flow onValueMismatch(ValueMatcher valueMatcher, ActualPath actualPath, Object actualValue, String message) {807 validationResult.addMismatch(message);808 return ExpectationHandler.Flow.PassToNext;809 }810 };811 // 1. validate using user provided validation block812 // 2. validate status code813 // 3. if validation block throws exception,814 // we still validate status code to make sure user is aware of the status code problem815 try {816 R extracted = ExpectationHandlers.withAdditionalHandler(recordAndThrowHandler, () -> {817 Object returnedValue = validator.validate(header, body);818 return (R) extractOriginalValue(returnedValue);819 });820 ExpectationHandlers.withAdditionalHandler(recordAndThrowHandler, () -> {821 validateStatusCode(validationResult);822 return null;823 });824 HttpValidationHandlers.validate(validationResult);825 return extracted;826 } catch (Throwable e) {827 ExpectationHandlers.withAdditionalHandler(new ExpectationHandler() {828 @Override829 public Flow onValueMismatch(ValueMatcher valueMatcher, ActualPath actualPath, Object actualValue, String message) {830 validationResult.addMismatch(message);831 // another assertion happened before status code check832 // we discard it and throw status code instead833 if (e instanceof AssertionError) {834 throw new AssertionError('\n' + message);835 }836 // originally an exception happened,837 // so we combine its message with status code failure838 throw new AssertionError('\n' + message +839 "\n\nadditional exception message:\n" + e.getMessage(), e);840 }841 }, () -> {842 validateErrorsOnlyStatusCode(validationResult);843 return null;844 });845 throw e;846 }847 }848 private DataNode createBodyDataNodeAndMarkResponseInvalidWhenParsingError(HttpValidationResult validationResult) {849 DataNodeId id = new DataNodeId("body");850 HttpResponse response = validationResult.getResponse();851 if (!response.isBinary() && response.nullOrEmptyTextContent()) {852 return new StructuredDataNode(id, new TraceableValue(null));853 }854 if (response.isText()) {855 return new StructuredDataNode(id, new TraceableValue(response.getTextContent()));856 }857 if (response.isJson()) {858 return tryParseJsonAndReturnTextIfFails(validationResult, id, response.getTextContent());859 }860 return new StructuredDataNode(id, new TraceableValue(response.getBinaryContent()));861 }862 private DataNode tryParseJsonAndReturnTextIfFails(HttpValidationResult validationResult,863 DataNodeId id,864 String textContent) {865 try {866 Object object = JsonUtils.deserialize(textContent);867 return DataNodeBuilder.fromValue(id, object);868 } catch (JsonParseException e) {869 validationResult.setBodyParseErrorMessage(e.getMessage());870 validationResult.addMismatch("can't parse JSON response of " + validationResult.getFullUrl()871 + ": " + e.getMessage());872 return new StructuredDataNode(id,873 new TraceableValue("invalid JSON:\n" + textContent));874 }875 }876 private void validateStatusCode(HttpValidationResult validationResult) {877 DataNode statusCode = validationResult.getHeaderNode().statusCode;878 if (statusCode.getTraceableValue().getCheckLevel() != CheckLevel.None) {879 return;880 }881 statusCode.should(equal(defaultExpectedStatusCodeByRequest(validationResult)));882 }883 private void validateErrorsOnlyStatusCode(HttpValidationResult validationResult) {884 DataNode statusCode = validationResult.getHeaderNode().statusCode;885 if (statusCode.getTraceableValue().getCheckLevel() != CheckLevel.None) {886 return;887 }888 Integer statusCodeValue = (Integer) statusCode.getTraceableValue().getValue();889 if (statusCodeValue >= 200 && statusCodeValue < 300) {890 return;891 }892 statusCode.should(equal(defaultExpectedStatusCodeByRequest(validationResult)));893 }894 private Integer defaultExpectedStatusCodeByRequest(HttpValidationResult validationResult) {895 switch (validationResult.getRequestMethod()) {896 case "POST":897 return 201;898 case "PUT":899 case "DELETE":900 case "PATCH":901 return validationResult.hasResponseContent() ? 200 : 204;902 case "GET":903 default:904 return 200;905 }906 }907 private HttpResponse request(String method, String fullUrl,908 HttpHeader requestHeader,909 HttpRequestBody requestBody) {910 if (requestHeader == null) {911 throw new IllegalArgumentException("Request header is null, check your header provider is not returning null");912 }913 try {914 HttpURLConnection connection = createConnection(fullUrl);915 connection.setInstanceFollowRedirects(false);916 setRequestMethod(method, connection);917 connection.setConnectTimeout(getCfg().getHttpTimeout());918 connection.setReadTimeout(getCfg().getHttpTimeout());919 connection.setRequestProperty("Content-Type", requestBody.type());920 connection.setRequestProperty("Accept", requestBody.type());921 connection.setRequestProperty("User-Agent", getCfg().getUserAgent());922 requestHeader.forEachProperty(connection::setRequestProperty);923 if (! (requestBody instanceof EmptyRequestBody)) {924 validateRequestContent(requestBody);925 connection.setDoOutput(true);926 if (requestBody.isBinary()) {927 connection.getOutputStream().write(requestBody.asBytes());928 } else {929 IOUtils.write(requestBody.asString(), connection.getOutputStream(), UTF_8);930 }931 }932 return extractHttpResponse(connection);933 } catch (IOException e) {934 throw new RuntimeException("couldn't " + method + ": " + fullUrl, e);935 }936 }937 private void validateRequestContent(HttpRequestBody requestBody) {938 if (requestBody.type().contains("/json")) {939 validateJsonRequestContent(requestBody.asString());940 }941 }942 private void validateJsonRequestContent(String json) {943 JsonUtils.deserialize(json);944 }945 private HttpURLConnection createConnection(String fullUrl) {946 try {947 if (getCfg().isHttpProxySet()) {948 HostPort hostPort = new HostPort(getCfg().getHttpProxyConfigValue().getAsString());949 return (HttpURLConnection) new URL(fullUrl).openConnection(new Proxy(Proxy.Type.HTTP,950 new InetSocketAddress(hostPort.host, hostPort.port)));951 }952 return (HttpURLConnection) new URL(fullUrl).openConnection();953 } catch (IOException e) {954 throw new UncheckedIOException(e);955 }956 }957 private void setRequestMethod(String method, HttpURLConnection connection) throws ProtocolException {958 if (method.equals("PATCH")) {959 // Http(s)UrlConnection does not recognize PATCH, unfortunately, nor will it be added, see960 // https://bugs.openjdk.java.net/browse/JDK-8207840 .961 // The Oracle-recommended solution requires JDK 11's new java.net.http package.962 try {963 Object connectionTarget = connection;964 if (connection instanceof HttpsURLConnection) {965 final Field delegateField = HttpsURLConnectionImpl.class.getDeclaredField("delegate");966 delegateField.setAccessible(true);967 connectionTarget = delegateField.get(connection);968 }969 final Field f = HttpURLConnection.class.getDeclaredField("method");970 f.setAccessible(true);971 f.set(connectionTarget, "PATCH");972 } catch (IllegalAccessException | NoSuchFieldException e) {973 throw new RuntimeException("Failed to enable PATCH on HttpUrlConnection", e);974 }975 } else {976 connection.setRequestMethod(method);977 }978 }979 private HttpResponse extractHttpResponse(HttpURLConnection connection) throws IOException {980 HttpResponse httpResponse = new HttpResponse();981 populateResponseHeader(httpResponse, connection);982 InputStream inputStream = getInputStream(connection);983 httpResponse.setStatusCode(connection.getResponseCode());984 httpResponse.setContentType(connection.getContentType() != null ? connection.getContentType() : "");985 if (!httpResponse.isBinary()) {986 httpResponse.setTextContent(inputStream != null ? IOUtils.toString(inputStream, StandardCharsets.UTF_8) : "");987 } else {988 httpResponse.setBinaryContent(inputStream != null ? IOUtils.toByteArray(inputStream) : new byte[0]);989 }990 return httpResponse;991 }992 private InputStream getInputStream(HttpURLConnection connection) throws IOException {993 InputStream inputStream = connection.getResponseCode() < 400 ? connection.getInputStream() : connection.getErrorStream();994 if ("gzip".equals(connection.getContentEncoding())) {995 inputStream = new GZIPInputStream(inputStream);996 }997 return inputStream;998 }999 private void populateResponseHeader(HttpResponse httpResponse, HttpURLConnection connection) {1000 Map<CharSequence, CharSequence> header = new LinkedHashMap<>();1001 connection.getHeaderFields().forEach((key, values) -> {1002 if (!values.isEmpty()) {1003 header.put(key, values.get(0));1004 }1005 });1006 httpResponse.addHeader(header);1007 }1008 /**1009 * Response consist of DataNode and Traceable values, but we need to return a simple value that can be used for1010 * regular calculations and to drive test flow1011 *1012 * @param v value returned from a validation callback1013 * @return extracted regular value1014 */1015 private Object extractOriginalValue(Object v) {1016 if (v instanceof DataNode) {1017 return ((DataNode) v).get();1018 }1019 if (v instanceof TraceableValue) {1020 return ((TraceableValue) v).getValue();1021 }1022 if (v instanceof List) {1023 return ((List<?>) v).stream().map(this::extractOriginalValue).collect(toList());1024 }1025 return v;1026 }1027 private interface HttpCall {1028 HttpResponse execute(String fullUrl, HttpHeader fullHeader);1029 }1030 private static class HostPort {1031 private final String host;1032 private final int port;1033 private HostPort(String hostPort) {1034 String[] parts = hostPort.split(":");1035 if (parts.length != 2) {1036 throw new IllegalArgumentException("expect host:port format for proxy, given: " + hostPort);1037 }1038 host = parts[0];1039 port = Integer.parseInt(parts[1]);1040 }1041 }1042 private static class BeforeFirstHttpCallListenerTrigger {...
Source:GraphQL.java
...20import org.testingisdocumenting.webtau.data.traceable.CheckLevel;21import org.testingisdocumenting.webtau.graphql.config.GraphQLHttpConfigurations;22import org.testingisdocumenting.webtau.graphql.listener.GraphQLListeners;23import org.testingisdocumenting.webtau.graphql.model.GraphQLRequest;24import org.testingisdocumenting.webtau.http.HttpHeader;25import org.testingisdocumenting.webtau.http.validation.HttpResponseValidator;26import org.testingisdocumenting.webtau.http.validation.HttpResponseValidatorIgnoringReturn;27import org.testingisdocumenting.webtau.http.validation.HttpResponseValidatorWithReturn;28public class GraphQL {29 public static final GraphQL graphql = new GraphQL();30 static final String GRAPHQL_URL = "/graphql";31 private static final HttpResponseValidatorWithReturn EMPTY_RESPONSE_VALIDATOR = (header, body) -> null;32 private static final int SUCCESS_CODE = 200;33 private static GraphQLSchema schema;34 private static GraphQLCoverage coverage;35 static GraphQLCoverage getCoverage() {36 return coverage;37 }38 public static GraphQLSchema getSchema() {39 return schema;40 }41 static void reset() {42 schema = new GraphQLSchema();43 coverage = new GraphQLCoverage(schema);44 }45 public void execute(String query) {46 execute(query, EMPTY_RESPONSE_VALIDATOR);47 }48 public void execute(String query, HttpResponseValidator validator) {49 execute(query, new HttpResponseValidatorIgnoringReturn(validator));50 }51 public <E> E execute(String query, HttpResponseValidatorWithReturn validator) {52 return execute(query, null, null, HttpHeader.EMPTY, validator);53 }54 public void execute(String query, HttpHeader header) {55 execute(query, header, EMPTY_RESPONSE_VALIDATOR);56 }57 public void execute(String query, HttpHeader header, HttpResponseValidator validator) {58 execute(query, null, null, header, new HttpResponseValidatorIgnoringReturn(validator));59 }60 public <E> E execute(String query, HttpHeader header, HttpResponseValidatorWithReturn validator) {61 return execute(query, null, null, header, validator);62 }63 public void execute(String query, String operationName) {64 execute(query, operationName, EMPTY_RESPONSE_VALIDATOR);65 }66 public void execute(String query, String operationName, HttpResponseValidator validator) {67 execute(query, null, operationName, HttpHeader.EMPTY, new HttpResponseValidatorIgnoringReturn(validator));68 }69 public <E> E execute(String query, String operationName, HttpResponseValidatorWithReturn validator) {70 return execute(query, null, operationName, HttpHeader.EMPTY, validator);71 }72 public void execute(String query, String operationName, HttpHeader header) {73 execute(query, operationName, header, EMPTY_RESPONSE_VALIDATOR);74 }75 public void execute(String query, String operationName, HttpHeader header, HttpResponseValidator validator) {76 execute(query, null, operationName, header, new HttpResponseValidatorIgnoringReturn(validator));77 }78 public <E> E execute(String query, String operationName, HttpHeader header, HttpResponseValidatorWithReturn validator) {79 return execute(query, null, operationName, header, validator);80 }81 public void execute(String query, Map<String, Object> variables) {82 execute(query, variables, EMPTY_RESPONSE_VALIDATOR);83 }84 public void execute(String query, Map<String, Object> variables, HttpResponseValidator validator) {85 execute(query, variables, null, HttpHeader.EMPTY, new HttpResponseValidatorIgnoringReturn(validator));86 }87 public <E> E execute(String query, Map<String, Object> variables, HttpResponseValidatorWithReturn validator) {88 return execute(query, variables, null, HttpHeader.EMPTY, validator);89 }90 public void execute(String query, Map<String, Object> variables, HttpHeader header) {91 execute(query, variables, header, EMPTY_RESPONSE_VALIDATOR);92 }93 public void execute(String query, Map<String, Object> variables, HttpHeader header, HttpResponseValidator validator) {94 execute(query, variables, null, header, new HttpResponseValidatorIgnoringReturn(validator));95 }96 public <E> E execute(String query, Map<String, Object> variables, HttpHeader header, HttpResponseValidatorWithReturn validator) {97 return execute(query, variables, null, header, validator);98 }99 public void execute(String query, Map<String, Object> variables, String operationName) {100 execute(query, variables, operationName, EMPTY_RESPONSE_VALIDATOR);101 }102 public void execute(String query, Map<String, Object> variables, String operationName, HttpResponseValidator validator) {103 execute(query, variables, operationName, HttpHeader.EMPTY, new HttpResponseValidatorIgnoringReturn(validator));104 }105 public <E> E execute(String query, Map<String, Object> variables, String operationName, HttpResponseValidatorWithReturn validator) {106 return execute(query, variables, operationName, HttpHeader.EMPTY, validator);107 }108 public void execute(String query, Map<String, Object> variables, String operationName, HttpHeader header) {109 execute(query, variables, operationName, header, EMPTY_RESPONSE_VALIDATOR);110 }111 public void execute(String query, Map<String, Object> variables, String operationName, HttpHeader header, HttpResponseValidator validator) {112 execute(query, variables, operationName, header, new HttpResponseValidatorIgnoringReturn(validator));113 }114 public <E> E execute(String query, Map<String, Object> variables, String operationName, HttpHeader header, HttpResponseValidatorWithReturn validator) {115 BeforeFirstGraphQLQueryListenerTrigger.trigger();116 GraphQLListeners.beforeGraphQLQuery(query, variables, operationName, header);117 GraphQLRequest graphQLRequest = new GraphQLRequest(query, variables, operationName);118 String url = GraphQLHttpConfigurations.requestUrl(GRAPHQL_URL, graphQLRequest);119 return http.post(url, header, graphQLRequest.toHttpRequestBody(), (headerDataNode, body) -> {120 Object validatorReturnValue = validator.validate(headerDataNode, body);121 if (headerDataNode.statusCode.getTraceableValue().getCheckLevel() == CheckLevel.None) {122 headerDataNode.statusCode.should(equal(SUCCESS_CODE));123 }124 return validatorReturnValue;125 });126 }127 private static class BeforeFirstGraphQLQueryListenerTrigger {128 static {...
Source:GraphQLTestBase.java
...17import org.junit.After;18import org.junit.AfterClass;19import org.junit.Before;20import org.junit.BeforeClass;21import org.testingisdocumenting.webtau.http.HttpHeader;22import org.testingisdocumenting.webtau.http.config.WebTauHttpConfiguration;23import org.testingisdocumenting.webtau.http.config.WebTauHttpConfigurations;24import org.testingisdocumenting.webtau.http.datanode.DataNode;25import org.testingisdocumenting.webtau.http.validation.HttpResponseValidator;26import org.testingisdocumenting.webtau.http.validation.HttpResponseValidatorWithReturn;27import org.testingisdocumenting.webtau.utils.CollectionUtils;28import org.testingisdocumenting.webtau.utils.UrlUtils;29import java.util.Map;30import java.util.function.Consumer;31import static org.testingisdocumenting.webtau.Matchers.actual;32import static org.testingisdocumenting.webtau.Matchers.equal;33public class GraphQLTestBase implements WebTauHttpConfiguration {34 protected static final GraphQLTestDataServer testServer = new GraphQLTestDataServer();35 protected final static String QUERY = "{ taskById(id: \"a\") { id } }";36 protected final static String MULTI_OP_QUERY = "query task { taskById(id: \"a\") { id } } " +37 "query openTasks { allTasks(uncompletedOnly: true) { id } }";38 protected final static String OP_NAME = "task";39 protected final static String QUERY_WITH_VARS = "query task($id: ID!) { taskById(id: $id) { id } }";40 protected final static Map<String, Object> VARS = CollectionUtils.aMapOf("id", "a");41 protected final static String MULTI_OP_QUERY_WITH_VARS = "query task($id: ID!) { taskById(id: $id) { id } } " +42 "query openTasks { allTasks(uncompletedOnly: true) { id } }";43 protected final static String ERROR_QUERY = "query error($msg: String!) { error(msg: $msg) { msg } }";44 protected final static HttpResponseValidator VALIDATOR = (header, body) -> body.get("data.taskById.id").should(equal("a"));45 protected final static HttpResponseValidatorWithReturn VALIDATOR_WITH_RETURN = (header, body) -> {46 body.get("data.taskById.id").should(equal("a"));47 return body.get("data.taskById.id");48 };49 protected final static Consumer<String> ID_ASSERTION = id -> actual(id).should(equal("a"));50 protected final static Consumer<DataNode> BODY_ASSERTION = body -> body.get("data.taskById.id").should(equal("a"));51 protected final static String AUTH_HEADER_VALUE = "aSuperSecretToken";52 protected final static HttpHeader AUTH_HEADER = HttpHeader.EMPTY.with("Authorization", AUTH_HEADER_VALUE);53 @BeforeClass54 public static void startServer() {55 testServer.start();56 }57 @AfterClass58 public static void stopServer() {59 testServer.stop();60 }61 @Before62 public void initCfg() {63 WebTauHttpConfigurations.add(this);64 }65 @After66 public void cleanCfg() {67 WebTauHttpConfigurations.remove(this);68 }69 @Override70 public String fullUrl(String url) {71 if (UrlUtils.isFull(url)) {72 return url;73 }74 return UrlUtils.concat(testServer.getUri(), url);75 }76 @Override77 public HttpHeader fullHeader(String fullUrl, String passedUrl, HttpHeader given) {78 return given;79 }80}...
HttpHeader
Using AI Code Generation
1package org.testingisdocumenting.webtau.http;2public class HttpHeader {3 private final String name;4 private final String value;5 public HttpHeader(String name, String value) {6 this.name = name;7 this.value = value;8 }9 public String getName() {10 return name;11 }12 public String getValue() {13 return value;14 }15}16package org.testingisdocumenting.webtau.http;17import org.testingisdocumenting.webtau.http.HttpHeader;18public class HttpHeader {19 private final String name;20 private final String value;21 public HttpHeader(String name, String value) {22 this.name = name;23 this.value = value;24 }25 public String getName() {26 return name;27 }28 public String getValue() {29 return value;30 }31}32package org.testingisdocumenting.webtau.http;33import org.testingisdocumenting.webtau.http.HttpHeader;34public class HttpHeader {35 private final String name;36 private final String value;37 public HttpHeader(String name, String value) {38 this.name = name;39 this.value = value;40 }41 public String getName() {42 return name;43 }44 public String getValue() {45 return value;46 }47}48package org.testingisdocumenting.webtau.http;49import org.testingisdocumenting.webtau.http.HttpHeader;50public class HttpHeader {51 private final String name;52 private final String value;53 public HttpHeader(String name, String value) {54 this.name = name;55 this.value = value;56 }57 public String getName() {58 return name;59 }60 public String getValue() {61 return value;62 }63}64package org.testingisdocumenting.webtau.http;65import org.testingisdocumenting.webtau.http.HttpHeader;66public class HttpHeader {67 private final String name;68 private final String value;69 public HttpHeader(String name,
HttpHeader
Using AI Code Generation
1package org.testingisdocumenting.webtau.http;2import org.testingisdocumenting.webtau.Ddjt;3import static org.testingisdocumenting.webtau.http.Http.http;4public class HttpHeader {5 public static void main(String[] args) {6 Ddjt.create("http header", (gwt) -> {7 gwt.before(() -> {8 http.get("/header", (header) -> {9 header.statusCode(200);10 header.header("X-Response-Header", "value");11 });12 });13 gwt.after(() -> {14 http.get("/header", (header) -> {15 header.statusCode(200);16 header.header("X-Response-Header", "value");17 });18 });19 gwt.test("http header", () -> {20 http.get("/header", (header) -> {21 header.statusCode(200);22 header.header("X-Response-Header", "value");23 });24 });25 });26 }27}
HttpHeader
Using AI Code Generation
1import org.testingisdocumenting.webtau.http.HttpHeader;2import org.testingisdocumenting.webtau.http.HttpHeaderEntry;3import static org.testingisdocumenting.webtau.Ddjt.*;4public class 1 {5 public static void main(String[] args) {6 HttpHeader header = HttpHeader.header("Content-Type", "application/json");7 HttpHeaderEntry entry = header.entry("Content-Type");8 HttpHeaderEntry entry1 = header.entry("content-type");9 HttpHeaderEntry entry2 = header.entry("content-type", "application/json");10 HttpHeaderEntry entry3 = header.entry("content-type", "application/xml");11 http.header("Content-Type").should(equal("application/json"));12 http.header("content-type").should(equal("application/json"));13 http.header("content-type", "application/json").should(equal("application/json"));14 http.header("content-type", "application/xml").should(equal("application/xml"));15 }16}17import org.testingisdocumenting.webtau.http.HttpHeader;18import org.testingisdocumenting.webtau.http.HttpHeaderEntry;19import static org.testingisdocumenting.webtau.Ddjt.*;20public class 2 {21 public static void main(String[] args) {22 HttpHeader header = HttpHeader.header("Content-Type", "application/json");23 HttpHeaderEntry entry = header.entry("Content-Type");24 HttpHeaderEntry entry1 = header.entry("content-type");25 HttpHeaderEntry entry2 = header.entry("content-type", "application/json");26 HttpHeaderEntry entry3 = header.entry("content-type", "application/xml");27 http.header("Content-Type").should(equal("application/json"));28 http.header("content-type").should(equal("application/json"));29 http.header("content-type", "application/json").should(equal("application/json"));30 http.header("content-type", "application/xml").should(equal("application/xml"));31 }32}33import org.testingisdocumenting.webtau.http.HttpHeader;34import org.testingisdocumenting.webtau.http.HttpHeaderEntry;35import static org.testingisdocumenting.webtau.Ddjt.*;36public class 3 {37 public static void main(String[] args)
HttpHeader
Using AI Code Generation
1HttpHeader header = HttpHeader.header("Content-Type", "application/json");2HttpHeader header = HttpHeader.header("Content-Type", "application/json");3HttpHeader header = HttpHeader.header("Content-Type", "application/json");4HttpHeader header = HttpHeader.header("Content-Type", "application/json");5HttpHeader header = HttpHeader.header("Content-Type", "application/json");6HttpHeader header = HttpHeader.header("Content-Type", "application/json");7HttpHeader header = HttpHeader.header("Content-Type", "application/json");8HttpHeader header = HttpHeader.header("Content-Type", "application/json");9HttpHeader header = HttpHeader.header("Content-Type", "application/json");10HttpHeader header = HttpHeader.header("Content-Type", "application/json");11HttpHeader header = HttpHeader.header("Content-Type", "application/json");12HttpHeader header = HttpHeader.header("Content-Type", "application/json");13HttpHeader header = HttpHeader.header("Content-Type", "application/json");14HttpHeader header = HttpHeader.header("Content-Type", "application/json");15HttpHeader header = HttpHeader.header("Content-Type", "application/json");16HttpHeader header = HttpHeader.header("Content-Type", "application
HttpHeader
Using AI Code Generation
1import org.testingisdocumenting.webtau.http.HttpHeader;2public class 1 {3 public static void main(String[] args) {4 HttpHeader header = HttpHeader.header("Content-Type", "application/json");5 System.out.println(header);6 }7}8import org.testingisdocumenting.webtau.http.HttpHeader;9public class 2 {10 public static void main(String[] args) {11 HttpHeader header = HttpHeader.header("Content-Type", "application/json");12 System.out.println(header);13 }14}15import org.testingisdocumenting.webtau.http.HttpHeader;16public class 3 {17 public static void main(String[] args) {18 HttpHeader header = HttpHeader.header("Content-Type", "application/json");19 System.out.println(header);20 }21}22import org.testingisdocumenting.webtau.http.HttpHeader;23public class 4 {24 public static void main(String[] args) {25 HttpHeader header = HttpHeader.header("Content-Type", "application/json");26 System.out.println(header);27 }28}29import org.testingisdocumenting.webtau.http.HttpHeader;30public class 5 {31 public static void main(String[] args) {32 HttpHeader header = HttpHeader.header("Content-Type", "application/json");33 System.out.println(header);34 }35}36import org.testingisdocumenting.webtau.http.HttpHeader;37public class 6 {38 public static void main(String[] args) {39 HttpHeader header = HttpHeader.header("Content-Type", "application/json");40 System.out.println(header);41 }42}43import org.testingisdocumenting.webtau.http.HttpHeader;44public class 7 {45 public static void main(String[]
HttpHeader
Using AI Code Generation
1package org.testingisdocumenting.webtau.http;2import org.testingisdocumenting.webtau.Ddjt;3public class HttpHeader {4 public static HttpHeader create(String name, String value) {5 return new HttpHeader(name, value);6 }7 private final String name;8 private final String value;9 private HttpHeader(String name, String value) {10 this.name = name;11 this.value = value;12 }13 public String getName() {14 return name;15 }16 public String getValue() {17 return value;18 }19 public HttpHeader shouldHaveName(String expectedName) {20 Ddjt.shouldEqual(expectedName, name);21 return this;22 }23 public HttpHeader shouldHaveValue(String expectedValue) {24 Ddjt.shouldEqual(expectedValue, value);25 return this;26 }27}28package org.testingisdocumenting.webtau.http;29import org.testingisdocumenting.webtau.Ddjt;30public class HttpHeader {31 public static HttpHeader create(String name, String value) {32 return new HttpHeader(name, value);33 }34 private final String name;35 private final String value;36 private HttpHeader(String name, String value) {37 this.name = name;38 this.value = value;39 }40 public String getName() {41 return name;42 }43 public String getValue() {44 return value;45 }46 public HttpHeader shouldHaveName(String expectedName) {47 Ddjt.shouldEqual(expectedName, name);48 return this;49 }50 public HttpHeader shouldHaveValue(String expectedValue) {51 Ddjt.shouldEqual(expectedValue, value);52 return this;53 }54}55package org.testingisdocumenting.webtau.http;56import org.testingisdocumenting.webtau.Ddjt;57public class HttpHeader {58 public static HttpHeader create(String name, String value) {59 return new HttpHeader(name, value);60 }61 private final String name;62 private final String value;63 private HttpHeader(String name, String value) {64 this.name = name;65 this.value = value;66 }67 public String getName() {68 return name;69 }
HttpHeader
Using AI Code Generation
1import org.testingisdocumenting.webtau.http.HttpHeader;2import org.testingisdocumenting.webtau.http.HttpHeaderName;3import org.testingisdocumenting.webtau.http.HttpHeaderValue;4HttpHeader header = HttpHeader.header(HttpHeaderName.of("headerName"), HttpHeaderValue.of("headerValue"));5HttpHeader header = HttpHeader.header("headerName", "headerValue");6HttpHeader header = HttpHeader.header("headerName", 1);7HttpHeader header = HttpHeader.header("headerName", 1.1);8HttpHeader header = HttpHeader.header("headerName", true);9HttpHeader header = HttpHeader.header("headerName", [1,2,3]);10HttpHeader header = HttpHeader.header("headerName", {"key": "value"});11HttpHeader header = HttpHeader.header("headerName", {"key": ["value1", "value2"]});12HttpHeader header = HttpHeader.header("headerName", {"key1": {"key2": "value"}});13HttpHeader header = HttpHeader.header("headerName", {"key1": {"key2": [1,2,3]}});14HttpHeader header = HttpHeader.header("headerName", {"key1": {"key2": {"key3": "value"}}});15HttpHeader header = HttpHeader.header("headerName", {"key1": {"key2": {"key3": [
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!