Repository: node-nock/nock (js)

Issue:
Stripping newlines from the body makes matching xml requests problematic.
For example the following xml

``` xml
<root><node>Some
text</node></root>
```

Is very much not the same as

``` xml
<root><node>Sometext</node></root>
```

And since I use a function to match the xml only the request body gets filtered.

I am currenty using `this.body.toString()` instead of the body parameter sent to the function as a workaround.

I see three obvious solutions.
1. Don't filter the new lines in the request body when a function is used to match.
   
   This makes sense since the person writing the matcher must otherwise know about this filtering.
2. Make a special case for xml content types and hope the code making the request actually cares to set it.
   
   This is similar to what was done for multipart requests in 014283b2
3. Change the filtering to normalize newlines to `\n` instead of removing them.
   
   This is what the [xml standard](https://www.w3.org/TR/REC-xml/#sec-line-ends) mandates that xml parsers to do _before_ parsing, and should work for any body data. 

I think _both_ 1 and 3 should be implemented, but I don't know enough about the project to make that call.

I would be glad to contribute a patch for any solution proposed by the people who actually knows why nock does things the way it does though!

Thanks.


Pull request:
fixing #533
**Problem:**
If the matcher for the body is a function the new lines in the body should not be stripped.
Because the developer writing the test should know that there are newline in the body.

Also there are API which expect newlines in the body, with the stripping it's hard to write tests for these APIs.

**Solution:**

I just change the order within match_body.js. This way a function `spec` will be used first and only if the spec is not a function the body will be altered and the new lines will be removed.

**Changes:**

- One new test case added
- Code order in match_body changed

closes #533

Patch:
diff --git a/.travis.yml b/.travis.yml
index 76f6bf3..d28b512 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,9 +1,14 @@
 sudo: false
 language: node_js
+cache:
+  directories:
+    - ~/.npm
+notifications:
+  email: false
 node_js:
-  - 4
-  - 6
-  - 8
+  - '8'
+  - '6'
+  - '4'
 
 # Retry install on fail to avoid failing a build on network/disk/external errors
 install:
@@ -13,9 +18,10 @@ script:
   - npm run lint
   - npm run unit
   - npm run integration
-after_script:
+after_success:
   - npm run coverage
   - npm run coveralls
+  - npm run semantic-release
 
 # Trigger a push build on master and greenkeeper branches + PRs build on every branches
 # Avoid double build on PRs (See https://github.com/travis-ci/travis-ci/issues/1147)
diff --git a/lib/match_body.js b/lib/match_body.js
index 4c1a44f..71f92bb 100644
--- a/lib/match_body.js
+++ b/lib/match_body.js
@@ -19,20 +19,6 @@ function matchBody(spec, body) {
 
   var isMultipart = contentType && contentType.toString().match(/multipart/);
 
-  //strip line endings from both so that we get a match no matter what OS we are running on
-  //if Content-Type does not contains 'multipart'
-  if (!isMultipart && typeof body === "string") {
-    body = body.replace(/\r?\n|\r/g, '');
-  }
-
-  if (spec instanceof RegExp) {
-    return body.match(spec);
-  }
-
-  if (!isMultipart && typeof spec === "string") {
-    spec = spec.replace(/\r?\n|\r/g, '');
-  }
-
   // try to transform body to json
   var json;
   if (typeof spec === 'object' || typeof spec === 'function') {
@@ -50,6 +36,20 @@ function matchBody(spec, body) {
     return spec.call(this, body);
   }
 
+  //strip line endings from both so that we get a match no matter what OS we are running on
+  //if Content-Type does not contains 'multipart'
+  if (!isMultipart && typeof body === "string") {
+    body = body.replace(/\r?\n|\r/g, '');
+  }
+
+  if (spec instanceof RegExp) {
+    return body.match(spec);
+  }
+
+  if (!isMultipart && typeof spec === "string") {
+    spec = spec.replace(/\r?\n|\r/g, '');
+  }
+
   return deepEqualExtended(spec, body);
 };
 
diff --git a/package.json b/package.json
index 6956f36..1c94ddd 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,7 @@
     "testing",
     "isolation"
   ],
-  "version": "9.0.22",
+  "version": "0.0.0-development",
   "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
   "contributors": [
     {
@@ -157,7 +157,7 @@
   ],
   "repository": {
     "type": "git",
-    "url": "http://github.com/node-nock/nock.git"
+    "url": "https://github.com/node-nock/nock.git"
   },
   "bugs": {
     "url": "http://github.com/node-nock/nock/issues"
@@ -200,7 +200,8 @@
     "semver": "^5.3.0",
     "superagent": "^3.5.0",
     "tap": "^10.0.0",
-    "zombie": "^5.0.1"
+    "zombie": "^5.0.1",
+    "semantic-release": "^8.2.0"
   },
   "scripts": {
     "unit": "tap --harmony ./tests/test_*.js",
@@ -211,7 +212,8 @@
     "coveralls": "cat ./coverage/lcov.info | coveralls",
     "lint": "eslint '**/*.js'",
     "changelog": "changelog nock all -m > CHANGELOG.md",
-    "toc": "markdown-toc -i README.md"
+    "toc": "markdown-toc -i README.md",
+    "semantic-release": "semantic-release pre && npm publish && semantic-release post"
   },
   "nyc": {
     "reporter": [


Test patch:
diff --git a/tests/test_common.js b/tests/test_common.js
index 3007d4f..c5b2662 100644
--- a/tests/test_common.js
+++ b/tests/test_common.js
@@ -12,6 +12,17 @@ tap.test('matchBody ignores new line characters from strings', function(t) {
   t.end()
 });
 
+tap.test('matchBody keeps new line characters if specs is a function', function(t) {
+  var body = "something //here is something more \n";
+  var bodyAsSpecParameter = null
+  var spec = function(bodyToTest) {
+    bodyAsSpecParameter = bodyToTest
+  }
+  matchBody(spec, body);
+  t.equal(bodyAsSpecParameter, body);
+  t.end()
+});
+
 tap.test('matchBody should not throw, when headers come node-fetch style as array', function(t) {
   var testThis = {
     headers: {