mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-06 19:19:56 +00:00
Allow piped server requests
This commit is contained in:
parent
2911caf7f8
commit
0b078be0f3
@ -7,7 +7,7 @@ connection = require('../../connection/connection')
|
|||||||
config = require('../config')
|
config = require('../config')
|
||||||
token = require('../token/token')
|
token = require('../token/token')
|
||||||
|
|
||||||
exports.request = (options = {}, callback, onProgress) ->
|
exports.request = (options = {}, outerCallback, onProgress) ->
|
||||||
|
|
||||||
onProgress ?= _.noop
|
onProgress ?= _.noop
|
||||||
|
|
||||||
@ -40,10 +40,16 @@ exports.request = (options = {}, callback, onProgress) ->
|
|||||||
_.extend options.headers,
|
_.extend options.headers,
|
||||||
'Authorization': "Bearer #{savedToken}"
|
'Authorization': "Bearer #{savedToken}"
|
||||||
|
|
||||||
progress(request(options, callback))
|
if options.pipe?
|
||||||
|
progress(request(options))
|
||||||
.on('progress', onProgress)
|
.on('progress', onProgress)
|
||||||
.on('error', callback)
|
.on('error', outerCallback)
|
||||||
.on('end', onProgress)
|
.on('end', onProgress)
|
||||||
|
.pipe(options.pipe)
|
||||||
|
.on('error', outerCallback)
|
||||||
|
.on('close', outerCallback)
|
||||||
|
else
|
||||||
|
return request(options, callback)
|
||||||
|
|
||||||
(response, body, callback) ->
|
(response, body, callback) ->
|
||||||
try
|
try
|
||||||
@ -54,7 +60,7 @@ exports.request = (options = {}, callback, onProgress) ->
|
|||||||
|
|
||||||
return callback(error, response, response.body)
|
return callback(error, response, response.body)
|
||||||
|
|
||||||
], callback
|
], outerCallback
|
||||||
|
|
||||||
createFacadeFunction = (method) ->
|
createFacadeFunction = (method) ->
|
||||||
lowerCaseMethod = method.toLowerCase()
|
lowerCaseMethod = method.toLowerCase()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
expect = require('chai').expect
|
expect = require('chai').expect
|
||||||
|
fs = require('fs')
|
||||||
nock = require('nock')
|
nock = require('nock')
|
||||||
url = require('url')
|
url = require('url')
|
||||||
sinon = require('sinon')
|
sinon = require('sinon')
|
||||||
@ -145,28 +146,31 @@ describe 'Server:', ->
|
|||||||
expect(response.body.status).to.equal(STATUS.ok)
|
expect(response.body.status).to.equal(STATUS.ok)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'should accept an onProgress function', (done) ->
|
it 'should allow piping files', (done) ->
|
||||||
spy = sinon.spy()
|
onProgressSpy = sinon.spy()
|
||||||
|
outputFile = '/hello'
|
||||||
|
|
||||||
server.request {
|
server.request {
|
||||||
method: 'GET'
|
method: 'GET'
|
||||||
url: URI.ok
|
url: URI.nojson
|
||||||
}, (error, response, body) ->
|
pipe: fs.createWriteStream(outputFile)
|
||||||
|
}, (error) ->
|
||||||
expect(error).to.not.exist
|
expect(error).to.not.exist
|
||||||
expect(spy).to.have.been.called
|
expect(onProgressSpy).to.have.been.called
|
||||||
|
|
||||||
|
fs.readFile outputFile, { encoding: 'utf8' }, (error, contents) ->
|
||||||
|
expect(error).to.not.exist
|
||||||
|
expect(contents).to.equal(RESPONSE.nojson)
|
||||||
done()
|
done()
|
||||||
, spy
|
, onProgressSpy
|
||||||
|
|
||||||
checkRequestTypeWithoutBody = (type) ->
|
checkRequestTypeWithoutBody = (type) ->
|
||||||
return (done) ->
|
return (done) ->
|
||||||
spy = sinon.spy()
|
|
||||||
lowercaseType = type.toLowerCase()
|
lowercaseType = type.toLowerCase()
|
||||||
server[lowercaseType] URI.ok, (error, response) ->
|
server[lowercaseType] URI.ok, (error, response) ->
|
||||||
return done(error) if error?
|
return done(error) if error?
|
||||||
expect(response.request.method).to.equal(type)
|
expect(response.request.method).to.equal(type)
|
||||||
expect(spy).to.have.been.called
|
|
||||||
done()
|
done()
|
||||||
, spy
|
|
||||||
|
|
||||||
describe '#get()', ->
|
describe '#get()', ->
|
||||||
it('should be a facade to request()', checkRequestTypeWithoutBody('GET'))
|
it('should be a facade to request()', checkRequestTypeWithoutBody('GET'))
|
||||||
@ -179,14 +183,11 @@ describe 'Server:', ->
|
|||||||
|
|
||||||
checkRequestTypeWithBody = (type, body) ->
|
checkRequestTypeWithBody = (type, body) ->
|
||||||
return (done) ->
|
return (done) ->
|
||||||
spy = sinon.spy()
|
|
||||||
lowercaseType = type.toLowerCase()
|
lowercaseType = type.toLowerCase()
|
||||||
server[lowercaseType] URI.ok, body, (error, response) ->
|
server[lowercaseType] URI.ok, body, (error, response) ->
|
||||||
return done(error) if error?
|
return done(error) if error?
|
||||||
expect(response.request.method).to.equal(type)
|
expect(response.request.method).to.equal(type)
|
||||||
expect(spy).to.have.been.called
|
|
||||||
done()
|
done()
|
||||||
, spy
|
|
||||||
|
|
||||||
describe '#post()', ->
|
describe '#post()', ->
|
||||||
it('should be a facade to request()', checkRequestTypeWithBody('POST', { hello: 'world' }))
|
it('should be a facade to request()', checkRequestTypeWithBody('POST', { hello: 'world' }))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user