From aba09c8f58d876b5080e804c18a628d98c559635 Mon Sep 17 00:00:00 2001 From: Gal Dubitski Date: Fri, 20 Jul 2018 13:08:36 +0100 Subject: [PATCH 1/7] adding form fields and file attachments support --- features/gherkin/request-options.feature | 42 +++++++++ package.json | 2 +- resources/blank.jpg | Bin 0 -> 631 bytes spec/support/http-client.spec.js | 105 ++++++++++++++++++++++ step-definitions/request-options.steps.js | 31 +++++++ support/http-client.js | 77 +++++++++++++--- 6 files changed, 243 insertions(+), 14 deletions(-) create mode 100644 resources/blank.jpg diff --git a/features/gherkin/request-options.feature b/features/gherkin/request-options.feature index f554a39..02d5bb3 100644 --- a/features/gherkin/request-options.feature +++ b/features/gherkin/request-options.feature @@ -37,6 +37,48 @@ Feature: Set request options When I send the request Then I expect last response to have status as "200" + Scenario: Set a form field + Given I request POST method at "https://httpbin.org/anything" url + And I set a field with name "field1" and value "value1" + When I send the request + Then I expect last response to have status as "200" + + Scenario: Set a form field as object + Given I request POST method at "https://httpbin.org/anything" url + And I set a field as object with name "field1" and value "value1" + When I send the request + Then I expect last response to have status as "200" + + Scenario: Set multiple form fields using data table + Given I request POST method at "https://httpbin.org/anything" url + And I set fields as: + | name | value | + | field1 | value1 | + | field2 | value2 | + When I send the request + Then I expect last response to have status as "200" + + Scenario: Set a file attachment as using local image + Given I request POST method at "https://httpbin.org/anything" url + And I set an attachment with name "photo" and path "./resources/blank.jpg" + When I send the request + Then I expect last response to have status as "200" + + Scenario: Set a file attachment as using base64 image + Given I request POST method at "https://httpbin.org/anything" url + And I set an attachment with name "image" and filename "pixel.gif" and buffer "data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" + When I send the request + Then I expect last response to have status as "200" + + Scenario: Set multiple file attachments using data table + Given I request POST method at "https://httpbin.org/anything" url + And I set attachments as: + | name | filename | buffer | + | image1 | black.gif | data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs= | + | image2 | transparent.gif | data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 | + When I send the request + Then I expect last response to have status as "200" + Scenario: Use generator functions in inline js template to POST method to send data to a url Given I request GET method at "https://httpbin.org/anything" url And I set "header1" header as "value1" diff --git a/package.json b/package.json index a23f60a..0c56d61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cucumber-steps-api", - "version": "0.1.3", + "version": "0.1.4", "description": "Cucumber step library for API testing and support functions", "main": "index.js", "scripts": { diff --git a/resources/blank.jpg b/resources/blank.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1cda9a53dc357ce07d3c67051b7615ebf7dc2f64 GIT binary patch literal 631 zcmex=^(PF6}rMnOeST|r4lSw=>~TvNxu(8R<ECr+Na zbot8FYu9hwy!G(W<0ns_J%91?)yGetzkL1n{m0K=Ab&A3Fhjfr_ZgbM1cClyVqsxs zVF&q(k*OSrnFU!`6%E;h90S=C3x$=88aYIqCNA7~kW<+>=!0ld(M2vX6_bamA3L7B$%azX<@d&d)*s literal 0 HcmV?d00001 diff --git a/spec/support/http-client.spec.js b/spec/support/http-client.spec.js index 025c1cb..b3c0f27 100644 --- a/spec/support/http-client.spec.js +++ b/spec/support/http-client.spec.js @@ -155,6 +155,111 @@ describe('Http Client', () => { }); }); + it('should support sending field with name and value in request', () => { + const client = new Client(agent); + return client + .url('https://httpbin.org/anything') + .method('post') + .field('field-name', 'field-value') + .send() + .then(response => { + expect(response.status).toBe(200); + }) + .catch(err => { + expect(err).toBeFalsy(); + }); + }); + + it('should support sending field as an object in request', () => { + const client = new Client(agent); + return client + .url('https://httpbin.org/anything') + .method('post') + .field({ name: 'field-name', value: 'field-value' }) + .send() + .then(response => { + expect(response.status).toBe(200); + }) + .catch(err => { + expect(err).toBeFalsy(); + }); + }); + + it('should support sending multiple fields in request', () => { + const client = new Client(agent); + return client + .url('https://httpbin.org/anything') + .method('post') + .field({ name: 'field1', value: 'value1' }) + .field({ name: 'field2', value: 'value2' }) + .send() + .then(response => { + expect(response.status).toBe(200); + }) + .catch(err => { + expect(err).toBeFalsy(); + }); + }); + + it('should support sending an attachment as { name, path } in request', () => { + const client = new Client(agent); + return client + .url('https://httpbin.org/anything') + .method('post') + .attach({ + name: 'photo', + path: './resources/blank.jpg' + }) + .send() + .then(response => { + expect(response.status).toBe(200); + }) + .catch(err => { + expect(err).toBeFalsy(); + }); + }); + + it('should support sending an attachment as { name, filename, buffer } in request', () => { + const client = new Client(agent); + const base64Image = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigD//2Q=='; + const buffer = new Buffer(base64Image, 'base64'); + return client + .url('https://httpbin.org/anything') + .method('post') + .attach({ + name: 'photo', + filename: 'photo.jpg', + buffer, + }) + .send() + .then(response => { + expect(response.status).toBe(200); + }) + .catch(err => { + expect(err).toBeFalsy(); + }); + }); + + it('should support sending multiple attachments in request', () => { + const client = new Client(agent); + const buffers = [ + new Buffer('data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=', 'base64'), + new Buffer('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64'), + ]; + return client + .url('https://httpbin.org/anything') + .method('post') + .attach({ name: 'image1', filename: 'black.gif', buffer: buffers[0] }) + .attach({ name: 'image2', filename: 'transparent.gif', buffer: buffers[1] }) + .send() + .then(response => { + expect(response.status).toBe(200); + }) + .catch(err => { + expect(err).toBeFalsy(); + }); + }); + it('should support sending accept type using header in request', () => { const client = new Client(agent); return client diff --git a/step-definitions/request-options.steps.js b/step-definitions/request-options.steps.js index 5c07af9..5331579 100644 --- a/step-definitions/request-options.steps.js +++ b/step-definitions/request-options.steps.js @@ -30,6 +30,37 @@ Given(/I set headers(?: as):/, function (dataTable) { return this.client.headers(dataTable.rowsHash()); }); +Given(/I set a field( as object)? with name "([^"]+)" and value "([^"]+)"/, function (isObject, name, value) { + expect(this.client).to.exist; + if (isObject) { + return this.client.field({ name, value }); + } + return this.client.field(name, value); +}); +Given(/I set fields(?: as):/, function (dataTable) { + expect(dataTable, 'data dataTable').to.exist; + expect(dataTable.rows().length > 0, 'data dataTable length > 0').to.be.true; + expect(this.client).to.exist; + const fields = dataTable.rows().map(row => ({ name: row[0], value: row[1] })); + return fields.reduce((client, field) => client.field(field), this.client); +}); + +Given(/I set an attachment with name "([^"]+)" and path "([^"]+)"/, function (name, path) { + expect(this.client).to.exist; + return this.client.attach({ name, path }); +}); +Given(/I set an attachment with name "([^"]+)" and filename "([^"]+)" and buffer "([^"]+)"/, function (name, filename, buffer) { + expect(this.client).to.exist; + return this.client.attach({ name, filename, buffer: new Buffer(buffer, 'base64') }); +}); +Given(/I set attachments(?: as):/, function (dataTable) { + expect(dataTable, 'data dataTable').to.exist; + expect(dataTable.rows().length > 0, 'data dataTable length > 0').to.be.true; + expect(this.client).to.exist; + const attachments = dataTable.rows().map(row => ({ name: row[0], filename: row[1], buffer: new Buffer(row[2], 'base64') })); + return attachments.reduce((client, attachment) => client.attach(attachment), this.client); +}); + Given(/I set "([^"]+)" query param as "([^"]+)"/, function (name, value) { expect(this.client, 'client').to.exist; return this.client.query(`${name}=${encodeURIComponent(value)}`); diff --git a/support/http-client.js b/support/http-client.js index 55281ee..e99d049 100644 --- a/support/http-client.js +++ b/support/http-client.js @@ -53,9 +53,9 @@ class RequestDefaults { const list = Array.isArray(headers) ? headers : Object.keys(headers).map(key => ({ - name: key, - value: headers[key], - })); + name: key, + value: headers[key], + })); list.forEach(header => this.header(header.name, header.value)); } return this; @@ -76,7 +76,7 @@ class RequestDefaults { queries(queries) { if (queries && queries.length) { this.$queries = this.$queries.concat( - queries.map(query => { + queries.map((query) => { if (typeof query === 'object' && Array.isArray(query)) { return [query[0], encodeURIComponent(query[1])].join('='); } else if (typeof query === 'object') { @@ -125,6 +125,8 @@ class RequestOptions extends RequestDefaults { this.$method = options.method; this.$url = options.url; this.$defaults = defaults || GlobalRequestDefaults; + this.$fields = []; + this.$attachments = []; } get defaults() { @@ -161,6 +163,20 @@ class RequestOptions extends RequestDefaults { return this; } + field(field) { + if (field) { + this.$fields.push(field); + } + return this; + } + + attach(attachment) { + if (attachment) { + this.$attachments.push(attachment); + } + return this; + } + dump(kind, wrap) { const lines = []; @@ -222,7 +238,7 @@ class RequestOptions extends RequestDefaults { `-d ${ typeof this.$body === 'object' ? JSON.stringify(this.$body) - : this.$body.toString() + : this.$body && this.$body.toString() }` ); } @@ -296,12 +312,22 @@ class HttpClient { this.$request.queries(queries); return this; } - body(body) { this.$request.body(body); return this; } - + field(name, value) { + if (typeof name === 'object') { + this.$request.field(name); + } else { + this.$request.field({ name, value }); + } + return this; + } + attach(attachment) { + this.$request.attach(attachment); + return this; + } key(key) { this.$request.key(key); return this; @@ -349,12 +375,12 @@ class HttpClient { this.$request.$defaults.$headers && this.$request.$defaults.$headers.length ) { - this.$request.$defaults.$headers.forEach(header => { + this.$request.$defaults.$headers.forEach((header) => { client = client.set(header.name, header.value); }); } if (this.$request.$headers && this.$request.$headers.length) { - this.$request.$headers.forEach(header => { + this.$request.$headers.forEach((header) => { client = client.set(header.name, header.value); }); } @@ -372,12 +398,12 @@ class HttpClient { this.$request.$defaults.$queries && this.$request.$defaults.$queries.length ) { - this.$request.$defaults.$queries.forEach(query => { + this.$request.$defaults.$queries.forEach((query) => { client = client.query(query); }); } if (this.$request.$queries && this.$request.$queries.length) { - this.$request.$queries.forEach(query => { + this.$request.$queries.forEach((query) => { client = client.query(query); }); } @@ -412,14 +438,39 @@ class HttpClient { ); } + if ( + this.$request.$attachments && + this.$request.$attachments.length && + ['post', 'put', 'patch'].includes(this.$request.$method) + ) { + this.$request.$attachments.forEach((attachment) => { + if (attachment.path) { + client = client.attach(attachment.name, attachment.path); + } else { + client = client.attach(attachment.name, attachment.buffer, attachment.filename); + } + }); + } + + if ( + this.$request.$fields && + this.$request.$fields.length && + ['post', 'put', 'patch'].includes(this.$request.$method) + ) { + this.$request.$fields.forEach((field) => { + client = client.field(field.name, field.value); + }); + } + if ( this.$request.$body && - !['get', 'head'].includes(this.$request.$method) + !['get', 'head'].includes(this.$request.$method) && + (this.$request.$fields.length === 0 || this.$request.$attachments.length === 0) ) { client = client.send(this.$request.$body); } - return client.ok(() => true).then(res => { + return client.ok(() => true).then((res) => { self.response = res; if (self.store) { self.store.put('last-response', res); From e4ecfef7de7e39b6ac2eefd78f4e8d6ef108a89e Mon Sep 17 00:00:00 2001 From: Sushil Nagi Date: Mon, 23 Jul 2018 12:07:47 +0100 Subject: [PATCH 2/7] Update request-options.steps.js --- step-definitions/request-options.steps.js | 28 ++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/step-definitions/request-options.steps.js b/step-definitions/request-options.steps.js index 5331579..5068d35 100644 --- a/step-definitions/request-options.steps.js +++ b/step-definitions/request-options.steps.js @@ -30,35 +30,37 @@ Given(/I set headers(?: as):/, function (dataTable) { return this.client.headers(dataTable.rowsHash()); }); -Given(/I set a field( as object)? with name "([^"]+)" and value "([^"]+)"/, function (isObject, name, value) { +Given(/I set "([^"]+)" field (?:with value|as) "([^"]+)"/, function (name, value) { expect(this.client).to.exist; - if (isObject) { - return this.client.field({ name, value }); - } return this.client.field(name, value); }); +Given(/I set "([^"]+)" field (?:with content|as):/, function (name, content) { + expect(this.client).to.exist; + return this.client.field(name, content); +}); Given(/I set fields(?: as):/, function (dataTable) { expect(dataTable, 'data dataTable').to.exist; expect(dataTable.rows().length > 0, 'data dataTable length > 0').to.be.true; expect(this.client).to.exist; const fields = dataTable.rows().map(row => ({ name: row[0], value: row[1] })); - return fields.reduce((client, field) => client.field(field), this.client); + return fields.forEach(field => this.client.field(field)); }); -Given(/I set an attachment with name "([^"]+)" and path "([^"]+)"/, function (name, path) { +Given(/I attach a|the file at "([^"]+)"(?: path) with name "([^"]+)"/, function (path, name) { expect(this.client).to.exist; return this.client.attach({ name, path }); }); -Given(/I set an attachment with name "([^"]+)" and filename "([^"]+)" and buffer "([^"]+)"/, function (name, filename, buffer) { +Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? "([^"]+)" with|using "([^"]+)"(?:( base64)? data)/, function (name, filename, data, isBase64) { expect(this.client).to.exist; - return this.client.attach({ name, filename, buffer: new Buffer(buffer, 'base64') }); + return this.client.attach({ name, filename, buffer: new Buffer(data, isBase64 ? 'base64' : 'utf8') }); }); -Given(/I set attachments(?: as):/, function (dataTable) { - expect(dataTable, 'data dataTable').to.exist; - expect(dataTable.rows().length > 0, 'data dataTable length > 0').to.be.true; +Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? "([^"]+)" using "([^"]+)"(?: with "([^"]+)" encoding)?/, function (name, filename, buffer, encoding) { + expect(this.client).to.exist; + return this.client.attach({ name, filename, buffer: new Buffer(buffer, encoding || 'base64') }); +}); +Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? with "([^"]+)" encoding)? using content:/, function (name, filename, encoding, content) { expect(this.client).to.exist; - const attachments = dataTable.rows().map(row => ({ name: row[0], filename: row[1], buffer: new Buffer(row[2], 'base64') })); - return attachments.reduce((client, attachment) => client.attach(attachment), this.client); + return this.client.attach({ name, filename, buffer: new Buffer(content, encoding || 'utf8') }); }); Given(/I set "([^"]+)" query param as "([^"]+)"/, function (name, value) { From 2069a3fb72ecbaf720652db36e6a8043a4e7f2b9 Mon Sep 17 00:00:00 2001 From: Sushil Nagi Date: Mon, 23 Jul 2018 12:12:57 +0100 Subject: [PATCH 3/7] Update request-options.steps.js --- step-definitions/request-options.steps.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/step-definitions/request-options.steps.js b/step-definitions/request-options.steps.js index 5068d35..137ae8f 100644 --- a/step-definitions/request-options.steps.js +++ b/step-definitions/request-options.steps.js @@ -42,7 +42,7 @@ Given(/I set fields(?: as):/, function (dataTable) { expect(dataTable, 'data dataTable').to.exist; expect(dataTable.rows().length > 0, 'data dataTable length > 0').to.be.true; expect(this.client).to.exist; - const fields = dataTable.rows().map(row => ({ name: row[0], value: row[1] })); + const fields = dataTable.raw().map(row => ({ name: row[0], value: row[1] })); return fields.forEach(field => this.client.field(field)); }); From 550f564080b80a05662d06b84e415515bbc025f2 Mon Sep 17 00:00:00 2001 From: Sushil Nagi Date: Mon, 23 Jul 2018 12:15:38 +0100 Subject: [PATCH 4/7] Update http-client.spec.js --- spec/support/http-client.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/support/http-client.spec.js b/spec/support/http-client.spec.js index b3c0f27..2112c96 100644 --- a/spec/support/http-client.spec.js +++ b/spec/support/http-client.spec.js @@ -221,7 +221,7 @@ describe('Http Client', () => { it('should support sending an attachment as { name, filename, buffer } in request', () => { const client = new Client(agent); - const base64Image = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigD//2Q=='; + const base64Image = '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigD//2Q=='; const buffer = new Buffer(base64Image, 'base64'); return client .url('https://httpbin.org/anything') @@ -243,8 +243,8 @@ describe('Http Client', () => { it('should support sending multiple attachments in request', () => { const client = new Client(agent); const buffers = [ - new Buffer('data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=', 'base64'), - new Buffer('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64'), + new Buffer('R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=', 'base64'), + new Buffer('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64'), ]; return client .url('https://httpbin.org/anything') From a7c4f54b84a83b8fc935cb6170e15ffbb6b7f3b9 Mon Sep 17 00:00:00 2001 From: Gal Dubitski Date: Mon, 23 Jul 2018 14:07:58 +0100 Subject: [PATCH 5/7] refactored feature file --- features/gherkin/request-options.feature | 33 ++++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/features/gherkin/request-options.feature b/features/gherkin/request-options.feature index 02d5bb3..c31298e 100644 --- a/features/gherkin/request-options.feature +++ b/features/gherkin/request-options.feature @@ -39,43 +39,42 @@ Feature: Set request options Scenario: Set a form field Given I request POST method at "https://httpbin.org/anything" url - And I set a field with name "field1" and value "value1" - When I send the request - Then I expect last response to have status as "200" - - Scenario: Set a form field as object - Given I request POST method at "https://httpbin.org/anything" url - And I set a field as object with name "field1" and value "value1" + And I set "field1" field with value "value1" When I send the request Then I expect last response to have status as "200" Scenario: Set multiple form fields using data table Given I request POST method at "https://httpbin.org/anything" url And I set fields as: - | name | value | | field1 | value1 | | field2 | value2 | When I send the request Then I expect last response to have status as "200" - Scenario: Set a file attachment as using local image + Scenario: Attach a file using local image Given I request POST method at "https://httpbin.org/anything" url - And I set an attachment with name "photo" and path "./resources/blank.jpg" + And I attach a file at "./resources/blank.jpg" path with name "photo" When I send the request Then I expect last response to have status as "200" - Scenario: Set a file attachment as using base64 image + Scenario: Attach an image file using base64 Given I request POST method at "https://httpbin.org/anything" url - And I set an attachment with name "image" and filename "pixel.gif" and buffer "data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" + And I add an attachment with name "image" and filename as "pixel.gif" with "R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" base64 data When I send the request Then I expect last response to have status as "200" - Scenario: Set multiple file attachments using data table + Scenario: Attach an XML file using utf-8 encoding Given I request POST method at "https://httpbin.org/anything" url - And I set attachments as: - | name | filename | buffer | - | image1 | black.gif | data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs= | - | image2 | transparent.gif | data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 | + And I add an attachment with name "myxml" and filename as "test.xml" with encoding "utf-8" using content: + """ + + + Someone + Me + Test Message + This is a test message! + + """ When I send the request Then I expect last response to have status as "200" From 7211ee301ab330e675e5beda5f7716b5bab4e805 Mon Sep 17 00:00:00 2001 From: Sushil Nagi Date: Mon, 23 Jul 2018 17:09:34 +0100 Subject: [PATCH 6/7] Update request-options.steps.js --- step-definitions/request-options.steps.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/step-definitions/request-options.steps.js b/step-definitions/request-options.steps.js index 137ae8f..119c3e8 100644 --- a/step-definitions/request-options.steps.js +++ b/step-definitions/request-options.steps.js @@ -54,7 +54,7 @@ Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? "([^" expect(this.client).to.exist; return this.client.attach({ name, filename, buffer: new Buffer(data, isBase64 ? 'base64' : 'utf8') }); }); -Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? "([^"]+)" using "([^"]+)"(?: with "([^"]+)" encoding)?/, function (name, filename, buffer, encoding) { +Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? "([^"]+)" using "([^"]+)" buffer(?: with "([^"]+)" encoding)?/, function (name, filename, buffer, encoding) { expect(this.client).to.exist; return this.client.attach({ name, filename, buffer: new Buffer(buffer, encoding || 'base64') }); }); From db632fb75fff8b886f4ab86798367b85ed1fe64a Mon Sep 17 00:00:00 2001 From: Gal Dubitski Date: Mon, 23 Jul 2018 17:21:22 +0100 Subject: [PATCH 7/7] fixed regex --- features/gherkin/request-options.feature | 2 +- step-definitions/request-options.steps.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/features/gherkin/request-options.feature b/features/gherkin/request-options.feature index c31298e..4ff2e22 100644 --- a/features/gherkin/request-options.feature +++ b/features/gherkin/request-options.feature @@ -65,7 +65,7 @@ Feature: Set request options Scenario: Attach an XML file using utf-8 encoding Given I request POST method at "https://httpbin.org/anything" url - And I add an attachment with name "myxml" and filename as "test.xml" with encoding "utf-8" using content: + And I add an attachment with name "myxml" and filename as "test.xml" with "utf-8" encoding using content: """ diff --git a/step-definitions/request-options.steps.js b/step-definitions/request-options.steps.js index 119c3e8..5f7598c 100644 --- a/step-definitions/request-options.steps.js +++ b/step-definitions/request-options.steps.js @@ -50,15 +50,15 @@ Given(/I attach a|the file at "([^"]+)"(?: path) with name "([^"]+)"/, function expect(this.client).to.exist; return this.client.attach({ name, path }); }); -Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? "([^"]+)" with|using "([^"]+)"(?:( base64)? data)/, function (name, filename, data, isBase64) { +Given(/I add an attachment with name "([^"]+)" (?:and|having) filename(?: as)? "([^"]+)" (?:with|using) "([^"]+)"(?:( base64)? data)/, function (name, filename, data, isBase64) { expect(this.client).to.exist; return this.client.attach({ name, filename, buffer: new Buffer(data, isBase64 ? 'base64' : 'utf8') }); }); -Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? "([^"]+)" using "([^"]+)" buffer(?: with "([^"]+)" encoding)?/, function (name, filename, buffer, encoding) { +Given(/I add an attachment with name "([^"]+)" (?:and|having) filename(?: as)? "([^"]+)" using "([^"]+)" buffer(?: with "([^"]+)" encoding)?/, function (name, filename, buffer, encoding) { expect(this.client).to.exist; return this.client.attach({ name, filename, buffer: new Buffer(buffer, encoding || 'base64') }); }); -Given(/I add an attachment with name "([^"]+)" and|having filename(?: as)? with "([^"]+)" encoding)? using content:/, function (name, filename, encoding, content) { +Given(/I add an attachment with name "([^"]+)" (?:and|having) filename(?: as)? "([^"]+)"( with "([^"]+)" encoding)? using content:/, function (name, filename, encoding, content) { expect(this.client).to.exist; return this.client.attach({ name, filename, buffer: new Buffer(content, encoding || 'utf8') }); });