Skip to content

Commit

Permalink
User agent (#7)
Browse files Browse the repository at this point in the history
* added optional value userAgent

* updated test with userAgent

* New release version

* Updated docs with User Agent settings
  • Loading branch information
Xvier authored Jan 25, 2020
1 parent cb641f5 commit 386b8cf
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ If autocompletion does not work out of the box you can add the following line ab
```


## Example of command
## Example of basic command
```javascript
cy.downloadFile('https://library.concordia.ca/help/technology/recovering_saved_files.pdf','mydownloads','demo.pdf')
```

## In Version 1.5 you can now also pass in the User-Agent. If no User-Agent is passed it will give a default User-Agent called request.
```javascript
cy.downloadFile('https://library.concordia.ca/help/technology/recovering_saved_files.pdf','mydownloads','demo.pdf','MyCustomAgentName')
```

3 changes: 2 additions & 1 deletion cypress/integration/test-plugin/download-files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ describe('Cypress Downloadfile Testing', () => {
cy.downloadFile(
'https://speed.hetzner.de/100MB.bin',
'mydownloads',
'100MB.bin'
'100MB.bin',
'MyAgent'
)
})
it('Small Size test', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-downloadfile",
"version": "1.1.1",
"version": "1.1.5",
"description": "Cypress custom command to download files",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/addPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ const path = require('path')
export function downloadFile(args) {
const directory = args.directory
const cookieHeader = args.cookies.map(e => e.name + '=' + e.value).join(';')
const userAgent = args.userAgent || 'request'
const fileName = args.fileName
return new Promise((resolve, reject) => {
request(
{
url: args.url,
encoding: null,
headers: { Cookie: cookieHeader },
headers: { Cookie: cookieHeader, 'User-Agent': userAgent },
},
function(err, res, body) {
if (!res) {
Expand Down
3 changes: 2 additions & 1 deletion src/downloadFileCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Cypress.Commands.add('downloadFile', (url, dir, fileName) => {
Cypress.Commands.add('downloadFile', (url, dir, fileName, userAgent) => {
return cy.getCookies().then(cookies => {
return cy.task('downloadFile', {
url: url,
directory: dir,
cookies: cookies,
fileName: fileName,
userAgent: userAgent,
})
})
})
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ declare namespace Cypress {
* @example
* cy.downloadFile('http://demourl','example','demo.pdf')
*/
downloadFile(url: string, directory: string, filename:string): Chainable<any>
downloadFile(url: string, directory: string, filename:string, userAgent?:string): Chainable<any>
}
}

0 comments on commit 386b8cf

Please sign in to comment.