-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #841 from apiaryio/honzajavorek/multipart-fix
Modify multipart body and calculate Content-Length in the right order
- Loading branch information
Showing
7 changed files
with
255 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FORMAT: 1A | ||
|
||
# Testing 'application/json' Request API | ||
|
||
# POST /data | ||
|
||
+ Request (application/json) | ||
|
||
+ Body | ||
|
||
{"test": 42} | ||
|
||
+ Response 200 (application/json; charset=utf-8) | ||
|
||
+ Body | ||
|
||
{"test": "OK"} |
17 changes: 17 additions & 0 deletions
17
test/fixtures/request/application-x-www-form-urlencoded.apib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FORMAT: 1A | ||
|
||
# Testing 'application/x-www-form-urlencoded' Request API | ||
|
||
# POST /data | ||
|
||
+ Request (application/x-www-form-urlencoded) | ||
|
||
+ Body | ||
|
||
test=42 | ||
|
||
+ Response 200 (application/json; charset=utf-8) | ||
|
||
+ Body | ||
|
||
{"test": "OK"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
FORMAT: 1A | ||
|
||
# Testing 'multipart/form-data' Request API | ||
|
||
# POST /data | ||
|
||
+ Request (multipart/form-data;boundary=---BOUNDARY) | ||
|
||
+ Body | ||
|
||
---BOUNDARY | ||
Content-Disposition: form-data; name="text" | ||
Content-Type: text/plain | ||
|
||
test equals to 42 | ||
---BOUNDARY | ||
Content-Disposition: form-data; name="json"; filename="filename.json" | ||
Content-Type: application/json | ||
|
||
{"test": 42} | ||
---BOUNDARY-- | ||
|
||
+ Response 200 (application/json; charset=utf-8) | ||
|
||
+ Body | ||
|
||
{"test": "OK"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FORMAT: 1A | ||
|
||
# Testing 'text/plain' Request API | ||
|
||
# POST /data | ||
|
||
+ Request (text/plain) | ||
|
||
+ Body | ||
|
||
test equals to 42 | ||
|
||
+ Response 200 (application/json; charset=utf-8) | ||
|
||
+ Body | ||
|
||
{"test": "OK"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
{assert} = require('chai') | ||
bodyParser = require('body-parser') | ||
|
||
{runDreddWithServer, createServer} = require('./helpers') | ||
Dredd = require('../../src/dredd') | ||
|
||
|
||
describe('Sending \'application/json\' request', -> | ||
runtimeInfo = undefined | ||
contentType = 'application/json' | ||
|
||
beforeEach((done) -> | ||
app = createServer({bodyParser: bodyParser.text({type: contentType})}) | ||
app.post('/data', (req, res) -> | ||
res.json({test: 'OK'}) | ||
) | ||
|
||
path = './test/fixtures/request/application-json.apib' | ||
dredd = new Dredd({options: {path}}) | ||
|
||
runDreddWithServer(dredd, app, (err, info) -> | ||
runtimeInfo = info | ||
done(err) | ||
) | ||
) | ||
|
||
it('results in one request being delivered to the server', -> | ||
assert.isTrue(runtimeInfo.server.requestedOnce) | ||
) | ||
it('the request has the expected Content-Type', -> | ||
assert.equal(runtimeInfo.server.lastRequest.headers['content-type'], contentType) | ||
) | ||
it('the request has the expected format', -> | ||
body = runtimeInfo.server.lastRequest.body | ||
assert.deepEqual(JSON.parse(body), {test: 42}) | ||
) | ||
it('results in one passing test', -> | ||
assert.equal(runtimeInfo.dredd.stats.tests, 1) | ||
assert.equal(runtimeInfo.dredd.stats.passes, 1) | ||
) | ||
) | ||
|
||
|
||
describe('Sending \'multipart/form-data\' request', -> | ||
runtimeInfo = undefined | ||
contentType = 'multipart/form-data' | ||
|
||
beforeEach((done) -> | ||
path = './test/fixtures/request/multipart-form-data.apib' | ||
|
||
app = createServer({bodyParser: bodyParser.text({type: contentType})}) | ||
app.post('/data', (req, res) -> | ||
res.json({test: 'OK'}) | ||
) | ||
dredd = new Dredd({options: {path}}) | ||
|
||
runDreddWithServer(dredd, app, (err, info) -> | ||
runtimeInfo = info | ||
done(err) | ||
) | ||
) | ||
|
||
it('results in one request being delivered to the server', -> | ||
assert.isTrue(runtimeInfo.server.requestedOnce) | ||
) | ||
it('the request has the expected Content-Type', -> | ||
assert.include(runtimeInfo.server.lastRequest.headers['content-type'], 'multipart/form-data') | ||
) | ||
it('the request has the expected format', -> | ||
assert.equal(runtimeInfo.server.lastRequest.body, [ | ||
'---BOUNDARY' | ||
'Content-Disposition: form-data; name="text"' | ||
'Content-Type: text/plain' | ||
'' | ||
'test equals to 42' | ||
'---BOUNDARY' | ||
'Content-Disposition: form-data; name="json"; filename="filename.json"' | ||
'Content-Type: application/json' | ||
'' | ||
'{"test": 42}' | ||
'---BOUNDARY--' | ||
'' | ||
].join('\r\n')) | ||
) | ||
it('results in one passing test', -> | ||
assert.equal(runtimeInfo.dredd.stats.tests, 1) | ||
assert.equal(runtimeInfo.dredd.stats.passes, 1) | ||
) | ||
) | ||
|
||
|
||
describe('Sending \'application/x-www-form-urlencoded\' request', -> | ||
runtimeInfo = undefined | ||
contentType = 'application/x-www-form-urlencoded' | ||
|
||
beforeEach((done) -> | ||
path = './test/fixtures/request/application-x-www-form-urlencoded.apib' | ||
|
||
app = createServer({bodyParser: bodyParser.text({type: contentType})}) | ||
app.post('/data', (req, res) -> | ||
res.json({test: 'OK'}) | ||
) | ||
dredd = new Dredd({options: {path}}) | ||
|
||
runDreddWithServer(dredd, app, (err, info) -> | ||
runtimeInfo = info | ||
done(err) | ||
) | ||
) | ||
|
||
it('results in one request being delivered to the server', -> | ||
assert.isTrue(runtimeInfo.server.requestedOnce) | ||
) | ||
it('the request has the expected Content-Type', -> | ||
assert.equal(runtimeInfo.server.lastRequest.headers['content-type'], contentType) | ||
) | ||
it('the request has the expected format', -> | ||
assert.equal(runtimeInfo.server.lastRequest.body, 'test=42\n') | ||
) | ||
it('results in one passing test', -> | ||
assert.equal(runtimeInfo.dredd.stats.tests, 1) | ||
assert.equal(runtimeInfo.dredd.stats.passes, 1) | ||
) | ||
) | ||
|
||
|
||
describe('Sending \'text/plain\' request', -> | ||
runtimeInfo = undefined | ||
contentType = 'text/plain' | ||
|
||
beforeEach((done) -> | ||
path = './test/fixtures/request/text-plain.apib' | ||
|
||
app = createServer({bodyParser: bodyParser.text({type: contentType})}) | ||
app.post('/data', (req, res) -> | ||
res.json({test: 'OK'}) | ||
) | ||
dredd = new Dredd({options: {path}}) | ||
|
||
runDreddWithServer(dredd, app, (err, info) -> | ||
runtimeInfo = info | ||
done(err) | ||
) | ||
) | ||
|
||
it('results in one request being delivered to the server', -> | ||
assert.isTrue(runtimeInfo.server.requestedOnce) | ||
) | ||
it('the request has the expected Content-Type', -> | ||
assert.equal(runtimeInfo.server.lastRequest.headers['content-type'], contentType) | ||
) | ||
it('the request has the expected format', -> | ||
assert.equal(runtimeInfo.server.lastRequest.body, 'test equals to 42\n') | ||
) | ||
it('results in one passing test', -> | ||
assert.equal(runtimeInfo.dredd.stats.tests, 1) | ||
assert.equal(runtimeInfo.dredd.stats.passes, 1) | ||
) | ||
) |