Skip to content

Commit 43a2824

Browse files
author
Stephen Mathieson
committed
x-ray: Add #ua()
Closes #4.
1 parent 524ed37 commit 43a2824

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lib/request.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function driver(opts) {
2525
return function plugin(xray) {
2626

2727
xray.request = function request(url, fn) {
28-
superagent.get(url, function(err, res) {
28+
var req = superagent.get(url);
29+
var ua = xray.ua();
30+
if (ua) req.set('User-Agent', ua);
31+
req.end(function(err, res) {
2932
if (err) return fn(err);
3033
else return fn(null, res.text);
3134
});

lib/x-ray.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = Xray;
3737
function Xray(url) {
3838
if (!(this instanceof Xray)) return new Xray(url);
3939

40-
40+
this._ua = null;
4141
this._format = function(o) { return o };
4242
this._paginate = false;
4343
this._limit = Infinity;
@@ -140,7 +140,19 @@ Xray.prototype.prepare = function(str, fn) {
140140
return this;
141141
};
142142

143+
/**
144+
* Get/set the request user agent.
145+
*
146+
* @param {String} [ua]
147+
* @return {Xray|String}
148+
* @api public
149+
*/
143150

151+
Xray.prototype.ua = function(ua){
152+
if (!arguments.length) return this._ua;
153+
this._ua = ua;
154+
return this;
155+
};
144156

145157
/**
146158
* Paginate

test/x-ray.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ describe('x-ray', function() {
4444
})
4545
});
4646

47+
describe('#ua(str)', function(){
48+
it('should set the user agent', function(done){
49+
xray('http://whatsmyuseragent.com/')
50+
.ua('foo/bar')
51+
.select('h2.info')
52+
.run(function(err, ua){
53+
if (err) return done(err);
54+
assert('foo/bar' == ua);
55+
done();
56+
})
57+
})
58+
})
59+
4760
it('should support arrays', function(done) {
4861
xray('http://mat.io')
4962
.select(['.Header-list-item a'])

0 commit comments

Comments
 (0)