/
HeadRequest.ts
63 lines (59 loc) · 2.44 KB
/
HeadRequest.ts
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
import { Answerable, Dictionary, DynamicRecord } from '@serenity-js/core';
import { AxiosRequestConfig } from 'axios';
import { HTTPRequest } from './HTTPRequest';
/**
* @desc
* The HTTP HEAD method requests the headers that are returned if the specified resource
* would be requested with an HTTP GET method.
* Such a request can be done before deciding to download a large resource to save bandwidth, for example.
*
* @example <caption>File download test</caption>
* import { Actor } from '@serenity-js/core';
* import { CallAnApi, HeadRequest, LastResponse, Send } from '@serenity-js/rest'
* import { Ensure, equals } from '@serenity-js/assertions';
*
* const actor = Actor.named('Apisit').whoCan(CallAnApi.at('https://myapp.com/api'));
*
* actor.attemptsTo(
* Send.a(HeadRequest.to('/downloads/my-test-document.pdf')),
* Ensure.that(LastResponse.status(), equals(200)),
* Ensure.that(LastResponse.header('Content-Length'), equals(256)), // assuming we know the size of the document
* );
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
* @see https://tools.ietf.org/html/rfc7231#section-4.3.2
*
* @extends {HTTPRequest}
*/
export class HeadRequest extends HTTPRequest {
/**
* @desc
* Configures the object with a destination URI.
*
* When the `resourceUri` is not a fully qualified URL but a path, such as `/products/2`,
* it gets concatenated with the URL provided to the Axios instance
* when the {@link CallAnApi} {@link @serenity-js/core/lib/screenplay~Ability} was instantiated.
*
* @param {@serenity-js/core/lib/screenplay~Answerable<string>} resourceUri
* The URI where the {@link @serenity-js/core/lib/screenplay/actor~Actor}
* should send the {@link HTTPRequest}.
*
* @returns {HeadRequest}
*/
static to(resourceUri: Answerable<string>): HeadRequest {
return new HeadRequest(resourceUri);
}
/**
* @desc
* Overrides the default Axios request configuration provided
* when {@link CallAnApi} {@link @serenity-js/core/lib/screenplay~Ability} was instantiated.
*
* @param {Answerable<DynamicRecord<AxiosRequestConfig>>} config
* Axios request configuration overrides
*
* @returns {HeadRequest}
*/
using(config: Answerable<DynamicRecord<AxiosRequestConfig>>): HeadRequest {
return new HeadRequest(this.resourceUri, undefined, Dictionary.of(config));
}
}