Skip to content

Commit

Permalink
Merge pull request #311 from Westwing-Home-and-Living/mod_uri
Browse files Browse the repository at this point in the history
Simple request URI modification logic
  • Loading branch information
tomas committed Apr 22, 2020
2 parents 819240f + fb07a5d commit 0ef49f6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ For information about options that've changed, there's always [the changelog](ht
- `headers` : Object containing custom HTTP headers for request. Overrides defaults described below.
- `auth` : Determines what to do with provided username/password. Options are `auto`, `digest` or `basic` (default). `auto` will detect the type of authentication depending on the response headers.
- `stream_length`: When sending streams, this lets you manually set the Content-Length header --if the stream's bytecount is known beforehand--, preventing ECONNRESET (socket hang up) errors on some servers that misbehave when receiving payloads of unknown size. Set it to `0` and Needle will get and set the stream's length for you, or leave unset for the default behaviour, which is no Content-Length header for stream payloads.
- `localAddress` : <string>, IP address. Passed to http/https request. Local interface from witch the request should be emitted.
- `localAddress`: <string>, IP address. Passed to http/https request. Local interface from witch the request should be emitted.
- `uri_modifier`: Anonymous function taking request (or redirect location if following redirects) URI as an argument and modifying it given logic. It has to return a valid URI string for successful request.

Response options
----------------
Expand Down
10 changes: 9 additions & 1 deletion lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ Needle.prototype.setup = function(uri, options) {
for (var h in options.headers)
config.headers[h.toLowerCase()] = options.headers[h];

config.uri_modifier = get_option('uri_modifier', null);

return config;
}

Expand Down Expand Up @@ -435,6 +437,12 @@ Needle.prototype.should_follow = function(location, config, original) {

Needle.prototype.send_request = function(count, method, uri, config, post_data, out, callback) {

if (typeof config.uri_modifier === 'function') {
var modified_uri = config.uri_modifier(uri);
debug('Modifying request URI', uri + ' => ' + modified_uri);
uri = modified_uri;
}

var timer,
returned = 0,
self = this,
Expand Down Expand Up @@ -520,7 +528,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
if (config.follow_set_cookies) {
var request_cookies = cookies.read(config.headers['cookie']);
config.previous_resp_cookies = resp.cookies;
if (Object.keys(request_cookies).length || Object.keys(resp.cookies).length) {
if (Object.keys(request_cookies).length || Object.keys(resp.cookies || {}).length) {
config.headers['cookie'] = cookies.write(extend(request_cookies, resp.cookies));
}
} else if (config.headers['cookie']) {
Expand Down
46 changes: 46 additions & 0 deletions test/uri_modifier_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var needle = require('../'),
sinon = require('sinon'),
should = require('should'),
http = require('http'),
helpers = require('./helpers');

var port = 3456;

describe('uri_modifier config parameter function', function() {

var server, uri;

function send_request(mw, cb) {
return needle.get(uri, null, { uri_modifier: mw }, cb);
}

before(function(done){
server = helpers.server({ port: port }, done);
})

after(function(done) {
server.close(done);
})

describe('modifies uri', function(){

var path = '/foo/replace';

before(function() {
uri = 'localhost:' + port + path
});

it('should modify path', function(done) {
send_request(function(uri) {
return uri.replace('/replace', '');
}, function(err, res) {
should.not.exist(err);
should(res.req.path).be.exactly('/foo');
done();
});

});

})

})

0 comments on commit 0ef49f6

Please sign in to comment.