Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Allow to parse response after a file upload + gzip test #59

Merged
merged 5 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ class requestJson.JsonClient

# Send a POST request to path with given JSON as body.
post: (path, json, options, callback, parse = true) ->
@handleRequest 'POST', path, json, options, callback
@handleRequest 'POST', path, json, options, callback, parse


# Send a PUT request to path with given JSON as body.
put: (path, json, options, callback, parse = true) ->
@handleRequest 'PUT', path, json, options, callback
@handleRequest 'PUT', path, json, options, callback, parse


# Send a PATCH request to path with given JSON as body.
patch: (path, json, options, callback, parse = true) ->
@handleRequest 'PATCH', path, json, options, callback
@handleRequest 'PATCH', path, json, options, callback, parse


# Send a HEAD request to path. Expect no response body.
Expand All @@ -148,7 +148,7 @@ class requestJson.JsonClient

# Send a DELETE request to path.
del: (path, options, callback, parse = true) ->
@handleRequest 'DELETE', path, null, options, callback
@handleRequest 'DELETE', path, null, options, callback, parse


# Alias for del
Expand All @@ -161,9 +161,12 @@ class requestJson.JsonClient
# Use a read stream for that.
# If you use a stream, it must have a "path" attribute...
# ...with its path or filename
sendFile: (path, files, data, callback) ->
callback = data if typeof(data) is "function"
req = @post path, null, callback, false #do not parse
sendFile: (path, files, data, callback, parse = false) ->
if typeof(data) is "function"
callback = data
parse = callback

req = @post path, null, callback, parse

form = req.form()
unless typeof(data) is "function"
Expand Down Expand Up @@ -193,9 +196,12 @@ class requestJson.JsonClient
# Use a read stream for that.
# If you use a stream, it must have a "path" attribute...
# ...with its path or filename
putFile: (path, file, data, callback) ->
callback = data if typeof(data) is "function"
req = @put path, null, callback, false #do not parse
putFile: (path, file, data, callback, parse=false) ->
if typeof(data) is "function"
parse = callback or false
callback = data

req = @put path, null, {}, callback, parse

# file is a string so it is a file path
if typeof file is "string"
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"utility"
],
"license": "MIT",
"version": "0.6.0",
"version": "0.6.1",
"homepage": "https://github.com/mycozycloud/request-json/",
"bugs": {
"url": "https://github.com/mycozycloud/request-json/issues"
Expand All @@ -38,17 +38,18 @@
"main": "./main.js",
"dependencies": {
"depd": "1.1.0",
"request": "2.72.0"
"request": "2.74.0"
},
"devDependencies": {
"body-parser": "1.15.0",
"body-parser": "1.15.2",
"chai": "3.5.0",
"coffee-script": "1.10.0",
"coffeelint": "1.15.7",
"compression": "1.6.2",
"connect-multiparty": "2.0.0",
"express": "4.13.4",
"lie": "3.0.4",
"mocha": "2.4.5"
"express": "4.14.0",
"lie": "3.1.0",
"mocha": "2.5.3"
},
"scripts": {
"prepublish": "./node_modules/coffee-script/bin/coffee -cb main.coffee",
Expand Down
69 changes: 67 additions & 2 deletions tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ fs = require "fs"
path = require "path"
bodyParser = require 'body-parser'
multiparty = require 'connect-multiparty'
compression = require 'compression'
zlib = require 'zlib'

request = require("./main")
global.Promise ?= require 'lie'

Expand Down Expand Up @@ -42,7 +45,15 @@ fakeUploadServer = (url, dir, callback= -> ) ->
app.post url, (req, res) ->
for key, file of req.files
fs.renameSync file.path, dir + '/' + file.name
res.sendStatus 201
res.send {uploadSuccess: true}, 201

fakeGzipServer = ->
app = express()
app.use(compression(fiter: -> true))
app.get '/gzip-route', (req, res, next) ->
res.set 'Content-Encoding', 'gzip'
res.send gzipSuccess: "ok"
return app

rawBody = (req, res, next) ->
req.setEncoding 'utf8'
Expand Down Expand Up @@ -558,7 +569,7 @@ describe "Files", ->
@server.close()

it "When I send get request to server", (done) ->
stream = @client.saveFileAsStream 'test-file', (err, res, body) ->
stream = @client.saveFileAsStream 'test-file', (err, res, body) =>
should.not.exist err
res.statusCode.should.be.equal 200
done()
Expand Down Expand Up @@ -595,6 +606,36 @@ describe "Files", ->
resultStats = fs.statSync './up/README.md'
resultStats.size.should.equal fileStats.size

describe "client.sendFile (parse response)", ->

before ->
@app = fakeUploadServer '/test-file', './up'
@server = @app.listen 8888
@client = request.createClient "http://localhost:8888/"

after ->
for name in fs.readdirSync './up'
fs.unlinkSync(path.join './up', name)
fs.rmdirSync './up'
@server.close()

it "When I send post request to server and parse response", (done) ->
file = './README.md'
@client.sendFile 'test-file', file, (error, response, body) =>
should.not.exist error
response.statusCode.should.be.equal 201
@body = body
done()
, true

it "Then the correct file is uploaded", ->
fileStats = fs.statSync './README.md'
resultStats = fs.statSync './up/README.md'
resultStats.size.should.equal fileStats.size

it "And the response is parsed.", ->
@body.uploadSuccess.should.equal true


describe "client.sendFileFromStream", ->

Expand Down Expand Up @@ -938,3 +979,27 @@ describe "Set header on request", ->
should.exist @body.msg
@body.msg.should.equal "ok"


describe "Handle Gzipped response", ->

describe "client.get", ->

before ->
@app = fakeGzipServer()
@server = @app.listen 8888
@client = request.createClient "http://localhost:8888/"
@client.headers['Accept-Encoding'] = 'gzip'

after ->
@server.close()

it "When I send a get request and expect a gzipped response", (done) ->
@client.get "gzip-route/", (error, response, body) =>
should.not.exist error
response.statusCode.should.be.equal 200
@body = body
done()

it "Then I get gzipSuccess: 'ok' as answer.", ->
should.exist @body.gzipSuccess
@body.gzipSuccess.should.equal "ok"