File tree Expand file tree Collapse file tree 3 files changed +30
-2
lines changed Expand file tree Collapse file tree 3 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -25,7 +25,10 @@ function driver(opts) {
25
25
return function plugin ( xray ) {
26
26
27
27
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 ) {
29
32
if ( err ) return fn ( err ) ;
30
33
else return fn ( null , res . text ) ;
31
34
} ) ;
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ module.exports = Xray;
37
37
function Xray ( url ) {
38
38
if ( ! ( this instanceof Xray ) ) return new Xray ( url ) ;
39
39
40
-
40
+ this . _ua = null ;
41
41
this . _format = function ( o ) { return o } ;
42
42
this . _paginate = false ;
43
43
this . _limit = Infinity ;
@@ -140,7 +140,19 @@ Xray.prototype.prepare = function(str, fn) {
140
140
return this ;
141
141
} ;
142
142
143
+ /**
144
+ * Get/set the request user agent.
145
+ *
146
+ * @param {String } [ua]
147
+ * @return {Xray|String }
148
+ * @api public
149
+ */
143
150
151
+ Xray . prototype . ua = function ( ua ) {
152
+ if ( ! arguments . length ) return this . _ua ;
153
+ this . _ua = ua ;
154
+ return this ;
155
+ } ;
144
156
145
157
/**
146
158
* Paginate
Original file line number Diff line number Diff line change @@ -44,6 +44,19 @@ describe('x-ray', function() {
44
44
} )
45
45
} ) ;
46
46
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
+
47
60
it ( 'should support arrays' , function ( done ) {
48
61
xray ( 'http://mat.io' )
49
62
. select ( [ '.Header-list-item a' ] )
You can’t perform that action at this time.
0 commit comments