Skip to content

Commit

Permalink
fix: add more methods to graphql helper (#3787)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Aug 19, 2023
1 parent 401c3fb commit b6c7668
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/helpers/GraphQL.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ Prepares request for axios call

Returns **[object][2]** graphQLRequest

### amBearerAuthenticated

Adds a header for Bearer authentication

```js
// we use secret function to hide token from logs
I.amBearerAuthenticated(secret('heregoestoken'))
```

#### Parameters

- `accessToken` **([string][3] | CodeceptJS.Secret)** Bearer access token

### haveRequestHeaders

Sets request headers for all requests of this test

#### Parameters

- `headers` **[object][2]** headers list

### sendMutation

Send query to GraphQL endpoint over http
Expand Down
25 changes: 25 additions & 0 deletions lib/helper/GraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class GraphQL extends Helper {
'Content-Type': 'application/json',
});

request.headers = { ...this.headers, ...request.headers };

if (this.config.onRequest) {
await this.config.onRequest(request);
}
Expand Down Expand Up @@ -200,5 +202,28 @@ class GraphQL extends Helper {
_setRequestTimeout(newTimeout) {
this.options.timeout = newTimeout;
}

/**
* Sets request headers for all requests of this test
*
* @param {object} headers headers list
*/
haveRequestHeaders(headers) {
this.headers = { ...this.headers, ...headers };
}

/**
* Adds a header for Bearer authentication
*
* ```js
* // we use secret function to hide token from logs
* I.amBearerAuthenticated(secret('heregoestoken'))
* ```
*
* @param {string | CodeceptJS.Secret} accessToken Bearer access token
*/
amBearerAuthenticated(accessToken) {
this.haveRequestHeaders({ Authorization: `Bearer ${accessToken}` });
}
}
module.exports = GraphQL;
54 changes: 54 additions & 0 deletions test/graphql/GraphQL_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,58 @@ describe('GraphQL', () => {
});
});
});

describe('headers', () => {
it('should set headers for all requests multiple times', async () => {
I.haveRequestHeaders({ 'XY1-Test': 'xy1-first' });
I.haveRequestHeaders({ 'XY1-Test': 'xy1-second' });
I.haveRequestHeaders({ 'XY2-Test': 'xy2' });

const response = await I.sendQuery('{ user(id: 0) { id name email }}');

response.config.headers.should.have.property('XY1-Test');
response.config.headers['XY1-Test'].should.eql('xy1-second');

response.config.headers.should.have.property('XY2-Test');
response.config.headers['XY2-Test'].should.eql('xy2');

response.config.headers.should.have.property('X-Test');
response.config.headers['X-Test'].should.eql('test');
});

it('should override the header set for all requests', async () => {
I.haveRequestHeaders({ 'XY-Test': 'first' });

const response = await I.sendQuery('{ user(id: 0) { id name email }}');

response.config.headers.should.have.property('XY-Test');
response.config.headers['XY-Test'].should.eql('first');

response.config.headers.should.have.property('X-Test');
response.config.headers['X-Test'].should.eql('test');
});

it('should set Bearer authorization', async () => {
I.amBearerAuthenticated('token');
const response = await I.sendQuery('{ user(id: 0) { id name email }}');

response.config.headers.should.have.property('Authorization');
response.config.headers.Authorization.should.eql('Bearer token');

response.config.headers.should.have.property('X-Test');
response.config.headers['X-Test'].should.eql('test');
});

it('should set Bearer authorization multiple times', async () => {
I.amBearerAuthenticated('token1');
I.amBearerAuthenticated('token2');
const response = await I.sendQuery('{ user(id: 0) { id name email }}');

response.config.headers.should.have.property('Authorization');
response.config.headers.Authorization.should.eql('Bearer token2');

response.config.headers.should.have.property('X-Test');
response.config.headers['X-Test'].should.eql('test');
});
});
});

0 comments on commit b6c7668

Please sign in to comment.