Repository: node-nock/nock
Pull request #998 (2017-10-19 13:45:15)

Fix matchStringOrRegexp TypeError
Fixes `TypeError: Cannot read property 'match' of undefined` when `pattern` is a regex and `target` is undefined.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13793014/badge)](https://coveralls.io/builds/13793014)

Coverage remained the same at 92.666% when pulling **26d29b6f921d6dc51f1288cea861bb1b7abfec0a on serahhh:master** into **ea2fe9fd88451f0c8be0b0010c6e861fb64f1c57 on node-nock:master**.


Patch:
diff --git a/lib/common.js b/lib/common.js
index 1a43170..9aa8bfb 100644
--- a/lib/common.js
+++ b/lib/common.js
@@ -320,7 +320,7 @@ function percentEncode(str) {
 }
 
 function matchStringOrRegexp(target, pattern) {
-  var str = !_.isUndefined(target) && target.toString ? target.toString() : target;
+  var str = (!_.isUndefined(target) && target.toString && target.toString()) || '';
 
   return pattern instanceof RegExp  ? str.match(pattern) : str === String(pattern);
 }
diff --git a/lib/interceptor.js b/lib/interceptor.js
index bedc1ae..d7f7376 100755
--- a/lib/interceptor.js
+++ b/lib/interceptor.js
@@ -339,6 +339,8 @@ Interceptor.prototype.matchIndependentOfBody = function matchIndependentOfBody(o
         , path = options.path
         , proto = options.proto;
 
+    path = path.split('?')[0];
+
     if (this.scope.transformPathFunction) {
         path = this.scope.transformPathFunction(path);
     }


Test patch:
diff --git a/tests/test_common.js b/tests/test_common.js
index 3ed47a1..3007d4f 100644
--- a/tests/test_common.js
+++ b/tests/test_common.js
@@ -152,6 +152,9 @@ tap.test('matchStringOrRegexp', {only: true}, function (t) {
 
   t.true(common.matchStringOrRegexp(123, 123), 'true if pattern is number and target matches');
 
+  t.false(common.matchStringOrRegexp(undefined, 'to not match'), 'handle undefined target when pattern is string');
+  t.false(common.matchStringOrRegexp(undefined, /not/), 'handle undefined target when pattern is regex');
+
   t.ok(common.matchStringOrRegexp('to match', /match/), 'match if pattern is regex and target matches');
   t.false(common.matchStringOrRegexp('to match', /not/), 'false if pattern is regex and target doesn\'t match');
   t.end();
diff --git a/tests/test_intercept.js b/tests/test_intercept.js
index 962cf8f..1050545 100644
--- a/tests/test_intercept.js
+++ b/tests/test_intercept.js
@@ -5098,7 +5098,7 @@ test('match multiple paths to domain using regexp with allowUnmocked (#835)', fu
     .reply(200, imgResponse);
 
   var scope2 = nock(/google/, nockOpts)
-    .get(/search\?/)
+    .get(/search/)
     .reply(200, searchResponse);
 
 
@@ -5171,6 +5171,32 @@ test('multiple interceptors override headers from unrelated request', function (
   });
 });
 
+test('match when query is specified with allowUnmocked (#490)', function (t) {
+  nock.cleanAll();
+
+  var nockOpts = { allowUnmocked: true };
+  var searchResponse = 'Matched body';
+
+  var scope = nock('http://www.google.com/', nockOpts)
+    .get('/search')
+    .query({q: 'js'})
+    .reply(200, searchResponse);
+
+
+  mikealRequest.get('http://www.google.com', function (err, res, body) {
+    t.type(err, 'null');
+    t.equal(res.statusCode, 200);
+
+    mikealRequest.get('http://www.google.com/search?q=js', function (err, res, body) {
+      scope.done();
+      t.type(err, 'null');
+      t.equal(res.statusCode, 200);
+      t.equal(body, searchResponse);
+      t.end();
+    });
+  });
+});
+
 test("teardown", function(t) {
   var leaks = Object.keys(global)
     .splice(globalCount, Number.MAX_VALUE);