Skip to content

Commit

Permalink
Merge branch 'master' into test-coverage
Browse files Browse the repository at this point in the history
Conflicts:
	.gitignore
  • Loading branch information
simov committed Dec 7, 2014
2 parents 37e2268 + 90fbd7c commit 9c5fbd3
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
node_modules
coverage
.idea
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,13 @@
## Change Log

### v2.49.0 (2014/11/28)
- [#1295](https://github.com/request/request/pull/1295) fix(proxy): no-proxy false positive (@oliamb)
- [#1292](https://github.com/request/request/pull/1292) Upgrade `caseless` to 0.8.1 (@mmalecki)
- [#1276](https://github.com/request/request/pull/1276) Set transfer encoding for multipart/related to chunked by default (@simov)
- [#1275](https://github.com/request/request/pull/1275) Fix multipart content-type headers detection (@simov)
- [#1269](https://github.com/request/request/pull/1269) adds streams example for review (@tbuchok)
- [#1238](https://github.com/request/request/pull/1238) Add examples README.md (@simov)

### v2.48.0 (2014/11/12)
- [#1263](https://github.com/request/request/pull/1263) Fixed a syntax error / typo in README.md (@xna2)
- [#1253](https://github.com/request/request/pull/1253) Add multipart chunked flag (@simov, @nylen)
Expand Down
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -256,10 +256,11 @@ var formData = {
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
// Pass multiple values /w an Array
attachments: [
fs.createReadStream(__dirname + '/attacment1.jpg'),
fs.createReadStream(__dirname + '/attachment1.jpg'),
fs.createReadStream(__dirname + '/attachment2.jpg')
],
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
custom_file: {
value: fs.createReadStream('/dev/urandom'),
Expand All @@ -277,7 +278,7 @@ request.post({url:'http://service.com/upload', formData: formData}, function opt
});
```

For advanced cases, you can the form-data object itself via `r.form()`. This can be modified until the request is fired on the next cycle of the event-loop. (Note that this calling `form()` will clear the currently set form data for that request.)
For advanced cases, you can access the form-data object itself via `r.form()`. This can be modified until the request is fired on the next cycle of the event-loop. (Note that this calling `form()` will clear the currently set form data for that request.)

```javascript
// NOTE: Advanced use-case, for normal use see 'formData' usage above
Expand Down Expand Up @@ -313,7 +314,7 @@ Some variations in different HTTP implementations require a newline/CRLF before,
chunked: false,
data: [
{
'content-type': 'application/json',
'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
},
{ body: 'I am an attachment' }
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -7,7 +7,7 @@
"util",
"utility"
],
"version": "2.48.1",
"version": "2.49.1",
"author": "Mikeal Rogers <mikeal.rogers@gmail.com>",
"repository": {
"type": "git",
Expand All @@ -23,9 +23,9 @@
"main": "index.js",
"dependencies": {
"bl": "~0.9.0",
"caseless": "~0.7.0",
"caseless": "~0.8.0",
"forever-agent": "~0.5.0",
"form-data": "~0.1.0",
"form-data": "~0.2.0",
"json-stringify-safe": "~5.0.0",
"mime-types": "~1.0.1",
"node-uuid": "~1.4.0",
Expand Down
9 changes: 7 additions & 2 deletions request.js
Expand Up @@ -199,7 +199,8 @@ function getProxyFromURI(uri) {
}
} else {
noProxyItem = noProxyItem.replace(/^\.*/, '.')
if (hostname.indexOf(noProxyItem) === hostname.length - noProxyItem.length) {
var isMatchedAt = hostname.indexOf(noProxyItem)
if (isMatchedAt > -1 && isMatchedAt === hostname.length - noProxyItem.length) {
return null
}
}
Expand Down Expand Up @@ -1429,6 +1430,10 @@ Request.prototype.multipart = function (multipart) {
return chunked ? items.append(part) : items.push(new Buffer(part))
}

if (chunked) {
self.setHeader('transfer-encoding', 'chunked')
}

var headerName = self.hasHeader('content-type')
if (!headerName || self.headers[headerName].indexOf('multipart') === -1) {
self.setHeader('content-type', 'multipart/related; boundary=' + self.boundary)
Expand Down Expand Up @@ -1477,7 +1482,7 @@ Request.prototype.json = function (val) {

self._json = true
if (typeof val === 'boolean') {
if (typeof self.body === 'object') {
if (self.body !== undefined) {
self.body = safeStringify(self.body)
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
Expand Down
18 changes: 18 additions & 0 deletions tests/server.js
Expand Up @@ -77,6 +77,24 @@ exports.createPostValidator = function (text, reqContentType) {
}
return l
}
exports.createPostJSONValidator = function (value, reqContentType) {
var l = function (req, resp) {
var r = ''
req.on('data', function (chunk) {r += chunk})
req.on('end', function () {
var parsedValue = JSON.parse(r)
assert.deepEqual(parsedValue, value)
if (reqContentType) {
assert.ok(req.headers['content-type'])
assert.ok(~req.headers['content-type'].indexOf(reqContentType))
}
resp.writeHead(200, {'content-type':'application/json'})
resp.write(r)
resp.end()
})
}
return l
}
exports.createGetResponse = function (text, contentType) {
var l = function (req, resp) {
contentType = contentType || 'text/plain'
Expand Down
54 changes: 54 additions & 0 deletions tests/test-json-request.js
@@ -0,0 +1,54 @@
'use strict'

var server = require('./server')
, stream = require('stream')
, request = require('../index')
, tape = require('tape')

var s = server.createServer()

tape('setup', function(t) {
s.listen(s.port, function() {
t.end()
})
})

function testJSONValue(testId, value) {
tape('test ' + testId, function(t) {
var testUrl = '/' + testId
s.on(testUrl, server.createPostJSONValidator(value, 'application/json'))
var opts = {
method: 'PUT',
uri: s.url + testUrl,
json: true,
body: value
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.equal(resp.statusCode, 200)
t.deepEqual(body, value)
t.end()
})
})
}

testJSONValue('jsonNull', null)
testJSONValue('jsonTrue', true)
testJSONValue('jsonFalse', false)
testJSONValue('jsonNumber', -289365.2938)
testJSONValue('jsonString', 'some string')
testJSONValue('jsonArray', ['value1', 2, null, 8925.53289, true, false, ['array'], { object: 'property' }])
testJSONValue('jsonObject', {
trueProperty: true,
falseProperty: false,
numberProperty: -98346.34698,
stringProperty: 'string',
nullProperty: null,
arrayProperty: ['array'],
objectProperty: { object: 'property' }
})

tape('cleanup', function(t) {
s.close()
t.end()
})
12 changes: 8 additions & 4 deletions tests/test-multipart.js
Expand Up @@ -94,7 +94,7 @@ function runTest(t, a) {
if (a.json) {
reqOptions.json = true
}
request.post(reqOptions, function (err, res, body) {
request[a.method](reqOptions, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.deepEqual(body, a.json ? {status: 'done'} : 'done')
Expand All @@ -105,6 +105,7 @@ function runTest(t, a) {
})
}

var methods = ['post', 'get']
var cases = [
{name: '-json +array', args: {json: false, array: true}},
{name: '-json -array', args: {json: false, array: false}},
Expand All @@ -127,8 +128,11 @@ var cases = [
{name: '+json +headers -chunked', args: {json: true, headers: true, array: false, chunked: false}}
]

cases.forEach(function (test) {
tape('multipart related ' + test.name, function(t) {
runTest(t, test.args)
methods.forEach(function(method) {
cases.forEach(function (test) {
tape('multipart related ' + method + ' ' + test.name, function(t) {
test.args.method = method
runTest(t, test.args)
})
})
})
7 changes: 7 additions & 0 deletions tests/test-proxy.js
Expand Up @@ -131,6 +131,13 @@ if (process.env.TEST_PROXY_HARNESS) {
env : { http_proxy : s.url }
}, true)

runTest('http_proxy with length of one more than the URL', {
env: {
HTTP_PROXY : s.url,
NO_PROXY: 'elgoog1.com' // one more char than google.com
}
}, true)

runTest('NO_PROXY hostnames are case insensitive', {
env : {
HTTP_PROXY : s.url,
Expand Down

0 comments on commit 9c5fbd3

Please sign in to comment.