Skip to content

Commit 1172345

Browse files
authored
Merge pull request #211 from AthennaIO/develop
feat: add authorize method to test request
2 parents c0a1f10 + 2e9c9e3 commit 1172345

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/http",
3-
"version": "5.25.0",
3+
"version": "5.26.0",
44
"description": "The Athenna Http server. Built on top of fastify.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/testing/plugins/request/TestRequest.ts

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import type { InjectOptions } from '#src/types'
1414
import { TestResponse } from '#src/testing/plugins/request/TestResponse'
1515

1616
export class TestRequest extends Macroable {
17+
/**
18+
* Headers that will be defined in all requests.
19+
*/
20+
private headers: Record<string, string> = {}
21+
1722
/**
1823
* Japa assert class instance.
1924
*/
@@ -40,9 +45,12 @@ export class TestRequest extends Macroable {
4045
url: string,
4146
options: InjectOptions = {}
4247
): Promise<TestResponse> {
43-
return Server.request({ url, method: 'GET', ...options }).then(res =>
44-
this.createResponse(res)
45-
)
48+
return Server.request({
49+
url,
50+
method: 'GET',
51+
...this.headers,
52+
...options
53+
}).then(res => this.createResponse(res))
4654
}
4755

4856
/**
@@ -59,9 +67,12 @@ export class TestRequest extends Macroable {
5967
url: string,
6068
options: InjectOptions = {}
6169
): Promise<TestResponse> {
62-
return Server.request({ url, method: 'HEAD', ...options }).then(res =>
63-
this.createResponse(res)
64-
)
70+
return Server.request({
71+
url,
72+
method: 'HEAD',
73+
...this.headers,
74+
...options
75+
}).then(res => this.createResponse(res))
6576
}
6677

6778
/**
@@ -78,9 +89,12 @@ export class TestRequest extends Macroable {
7889
url: string,
7990
options: InjectOptions = {}
8091
): Promise<TestResponse> {
81-
return Server.request({ url, method: 'OPTIONS', ...options }).then(res =>
82-
this.createResponse(res)
83-
)
92+
return Server.request({
93+
url,
94+
method: 'OPTIONS',
95+
...this.headers,
96+
...options
97+
}).then(res => this.createResponse(res))
8498
}
8599

86100
/**
@@ -97,9 +111,12 @@ export class TestRequest extends Macroable {
97111
url: string,
98112
options: InjectOptions = {}
99113
): Promise<TestResponse> {
100-
return Server.request({ url, method: 'POST', ...options }).then(res =>
101-
this.createResponse(res)
102-
)
114+
return Server.request({
115+
url,
116+
method: 'POST',
117+
...this.headers,
118+
...options
119+
}).then(res => this.createResponse(res))
103120
}
104121

105122
/**
@@ -116,9 +133,12 @@ export class TestRequest extends Macroable {
116133
url: string,
117134
options: InjectOptions = {}
118135
): Promise<TestResponse> {
119-
return Server.request({ url, method: 'PUT', ...options }).then(res =>
120-
this.createResponse(res)
121-
)
136+
return Server.request({
137+
url,
138+
method: 'PUT',
139+
...this.headers,
140+
...options
141+
}).then(res => this.createResponse(res))
122142
}
123143

124144
/**
@@ -135,9 +155,12 @@ export class TestRequest extends Macroable {
135155
url: string,
136156
options: InjectOptions = {}
137157
): Promise<TestResponse> {
138-
return Server.request({ url, method: 'PATCH', ...options }).then(res =>
139-
this.createResponse(res)
140-
)
158+
return Server.request({
159+
url,
160+
method: 'PATCH',
161+
...this.headers,
162+
...options
163+
}).then(res => this.createResponse(res))
141164
}
142165

143166
/**
@@ -154,8 +177,28 @@ export class TestRequest extends Macroable {
154177
url: string,
155178
options: InjectOptions = {}
156179
): Promise<TestResponse> {
157-
return Server.request({ url, method: 'DELETE', ...options }).then(res =>
158-
this.createResponse(res)
159-
)
180+
return Server.request({
181+
url,
182+
method: 'DELETE',
183+
...this.headers,
184+
...options
185+
}).then(res => this.createResponse(res))
186+
}
187+
188+
/**
189+
* Define the authorization access token into all requests.
190+
*
191+
* @example
192+
* ```js
193+
* const token ='Bearer ...'
194+
* const response = await request.authorize(token).get('/users')
195+
*
196+
* response.assertStatusCode(200)
197+
* ```
198+
*/
199+
public authorize(accessToken: string) {
200+
this.headers.authorization = accessToken
201+
202+
return this
160203
}
161204
}

0 commit comments

Comments
 (0)