Repository: codemanki/cloudscraper (js)

Issue:
(confirmed bug) Support the baseUrl request option
Request will throw an error when the `baseUrl` option is set and `uri` is not a relative uri.
We would need to either remove the `baseUrl` option when setting `options.uri` to be the absolute challenge url or always set both `options.baseUrl` and `options.uri` but ensure that `uri` is a relative path.

Pull request:
Support the baseUrl option
Fixes #105 

Patch:
diff --git a/index.js b/index.js
index 8e228dc..dae35f7 100644
--- a/index.js
+++ b/index.js
@@ -255,9 +255,13 @@ function solveChallenge (options, response, body) {
 
   // Prevent reusing the headers object to simplify unit testing.
   options.headers = Object.assign({}, options.headers);
-  // Use the original uri as the referer and to construct the answer url.
+  // Use the original uri as the referer and to construct the answer uri.
   options.headers['Referer'] = uri.href;
   options.uri = uri.protocol + '//' + uri.hostname + '/cdn-cgi/l/chk_jschl';
+  // baseUrl can't be used in conjunction with an absolute uri
+  if (options.baseUrl !== undefined) {
+    options.baseUrl = undefined;
+  }
   // Set the query string and decrement the number of challenges to solve.
   options.qs = payload;
   options.challengesToSolve = options.challengesToSolve - 1;


Test patch:
diff --git a/test/test-index.js b/test/test-index.js
index 092c278..8b096d2 100644
--- a/test/test-index.js
+++ b/test/test-index.js
@@ -554,4 +554,40 @@ describe('Cloudscraper', function () {
     expect(custom.defaults).to.equal(cloudscraper.defaults);
     done();
   });
+
+  it('should not error when using baseUrl option', function (done) {
+    var cf = cloudscraper.defaults({ baseUrl: 'http://example-site.dev/' });
+
+    var firstParams = helper.extendParams({
+      baseUrl: 'http://example-site.dev/',
+      uri: '/test'
+    });
+
+    var firstResponse = helper.cloudflareResponse({
+      body: helper.getFixture('js_challenge_03_12_2018_2.html')
+    });
+
+    Request.onFirstCall()
+      .callsFake(helper.fakeRequest({ response: firstResponse }));
+
+    var secondResponse = helper.fakeResponse({ body: requestedPage });
+    var expectedBody = secondResponse.body.toString('utf8');
+
+    Request.onSecondCall()
+      .callsFake(helper.fakeRequest({ response: secondResponse }));
+
+    var promise = cf.get('/test', function (error, response, body) {
+      expect(error).to.be.null;
+
+      expect(Request).to.be.calledTwice;
+      expect(Request.firstCall).to.be.calledWithExactly(firstParams);
+      expect(Request.secondCall.args[0]).to.not.have.property('baseUrl');
+
+      expect(response).to.be.equal(secondResponse);
+      expect(body).to.be.equal(expectedBody);
+    });
+
+    expect(promise).to.eventually.equal(expectedBody).and.notify(done);
+    this.clock.tick(7000);
+  });
 });