Skip to content

Commit

Permalink
Implement baseUrl.
Browse files Browse the repository at this point in the history
  • Loading branch information
froatsnook committed Mar 10, 2015
1 parent 8109dc0 commit d13ccd2
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ request.get({
The first argument can be either a `url` or an `options` object. The only required option is `uri`; all others are optional.
* `uri` || `url` - fully qualified uri or a parsed url object from `url.parse()`
* `baseUrl` - fully qualified uri used as the base url. Most useful with `request.defaults`, for example when you want to do many requests to the same domain. If `baseUrl` is `https://example.com/api/`, then requesting `end/point?test=true` will fetch `https://example.com/api/end/point?test=true`.
* `qs` - object containing querystring values to be appended to the `uri`
* `useQuerystring` - If true, use `querystring` to stringify and parse
querystrings, otherwise use `qs` (default: `false`). Set this option to
Expand Down
22 changes: 22 additions & 0 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,28 @@ Request.prototype.init = function (options) {
delete self.url
}

// If there's a baseUrl, then use it as the base URL (i.e. uri must be
// specified as a relative path and is appended to baseUrl).
if (self.baseUrl) {
if (typeof self.baseUrl !== 'string') {
return self.emit('error', new Error('options.baseUrl must be a string'))
}

if (typeof self.uri !== 'string') {
return self.emit('error', new Error('options.uri must be a string when using options.baseUrl'))
}

if (self.uri.indexOf('/') === 0 || self.uri.indexOf('://') !== -1) {
return self.emit('error', new Error('options.uri must be a relative path when using options.baseUrl'))
}

if (self.baseUrl.lastIndexOf('/') === self.baseUrl.length - 1) {
self.uri = self.baseUrl + self.uri
} else {
self.uri = self.baseUrl + '/' + self.uri
}
}

// A URI is needed by this point, throw if we haven't been able to get one
if (!self.uri) {
return self.emit('error', new Error('options.uri is a required argument'))
Expand Down
110 changes: 110 additions & 0 deletions tests/test-baseUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
'use strict'

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

var s = http.createServer(function(req, res) {
res.statusCode = 200
res.setHeader('X-PATH', req.url)
res.end('ok')
})

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

tape('baseUrl', function(t) {
request('resource', {
baseUrl: 'http://localhost:6767'
}, function(err, resp, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.end()
})
})

tape('baseUrl defaults', function(t) {
var withDefaults = request.defaults({
baseUrl: 'http://localhost:6767'
})
withDefaults('resource', function(err, resp, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.end()
})
})

tape('baseUrl without path', function(t) {
request('resource', {
baseUrl: 'http://localhost:6767'
}, function(err, resp, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.equal(resp.headers['x-path'], '/resource')
t.end()
})
})

tape('baseUrl without path, with trailing slash', function(t) {
request('resource', {
baseUrl: 'http://localhost:6767/'
}, function(err, resp, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.equal(resp.headers['x-path'], '/resource')
t.end()
})
})

tape('baseUrl with path', function(t) {
request('resource', {
baseUrl: 'http://localhost:6767/path/to'
}, function(err, resp, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.equal(resp.headers['x-path'], '/path/to/resource')
t.end()
})
})

tape('baseUrl with path and trailing slash', function(t) {
request('resource', {
baseUrl: 'http://localhost:6767/path/to/'
}, function(err, resp, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.equal(resp.headers['x-path'], '/path/to/resource')
t.end()
})
})

tape('baseUrl with empty uri', function(t) {
request('', {
baseUrl: 'http://localhost:6767/path/to'
}, function(err, resp, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.equal(resp.headers['x-path'], '/path/to/')
t.end()
})
})

tape('baseUrl with trailing slash and empty uri', function(t) {
request('', {
baseUrl: 'http://localhost:6767/path/to/'
}, function(err, resp, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.equal(resp.headers['x-path'], '/path/to/')
t.end()
})
})

tape('cleanup', function(t) {
s.close(function() {
t.end()
})
})

0 comments on commit d13ccd2

Please sign in to comment.