Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

request.agent undefined #1

Closed
naholyr opened this issue May 19, 2014 · 5 comments
Closed

request.agent undefined #1

naholyr opened this issue May 19, 2014 · 5 comments
Assignees
Labels

Comments

@naholyr
Copy link

naholyr commented May 19, 2014

There is currently no way to create a persistent client (cookies & co) using supertest-as-promised.agent() whereas it's available in supertest

@benesch benesch self-assigned this May 19, 2014
@benesch benesch added the bug label May 19, 2014
@benesch
Copy link
Contributor

benesch commented May 19, 2014

We don't use the agent in our codebase, so I didn't bother to wrap it with promise support. Marking as a bug since you'd expect all SuperTest features to work. Thanks!

Shouldn't be too hard; I'll add it to the todo list. Feel free to open a PR in the meantime!

@naholyr
Copy link
Author

naholyr commented May 19, 2014

I tried the following, naively copying the wrapping logic and applying it to request.agent's result but it didn't do the trick:

function wrap (request) {
  // Wrap all SuperTest functions (`get`, `post`, etc.) so we can inject a
  // `then` method into the returned `Test` instance
  return _.mapValues(request, function wrap(fn) {
    return function () {
      var test = fn.apply(null, arguments);
      test.then = then;
      return test;
    };
  });
}

function _request () {
  // Let SuperTest work its magic on whatever arguments we receive
  return wrap(supertest.apply(null, arguments));
}

_request.agent = function () {
  // Let SuperTest work its magic on whatever arguments we receive
  return wrap(supertest.agent.apply(supertest, arguments));
}

module.exports = _request;

No time to dig yet

@naholyr
Copy link
Author

naholyr commented May 19, 2014

I solved it in my app by dropping this hack in my tests:

var Test = require('supertest/lib/test');
Test.prototype.then = function (onResolve, onReject, onProgress) {
  return Promise.promisify(this.end, this)().then(onResolve, onReject, onProgress);
}

Seems to apply better.

@benesch
Copy link
Contributor

benesch commented May 19, 2014

Thanks for the inspiration, @naholyr! The issue was that TestAgents defined .get, .post, etc. on their prototype, so _.mapValues was ignoring them. I refactored the code to always override all HTTP methods.

Your hack is elegant, but it duck-punches promise support into SuperTest application-wide. This is probably fine since adding a .then method isn't particularly intrusive, but I want to avoid it if possible. Works well when you control the whole application, though.

Let me know if you run into trouble.

@naholyr
Copy link
Author

naholyr commented May 19, 2014

Great job \o/

On 19 May 2014 19:35, Nikhil Benesch notifications@github.com wrote:

Thanks for the inspiration, @naholyr https://github.com/naholyr! The
issue was that TestAgents defined .get, .post, etc. on their prototype,
so _.mapValues was ignoring them. I refactored the code to always
override all HTTP methods.

Your hack is elegant, but it duck-punches promise support into SuperTest
application-wide. This is probably fine since adding a .then method isn't
particularly intrusive, but I want to avoid it if possible. Works well when
you control the whole application, though.

Let me know if you run into trouble.


Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-43533362
.

Nicolas Chambrier, aka naholyr

Blog : http://naholyr.fr
Formateur Clever Institut :
http://clever-institut.com/formateur/nicolas-chambrier

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants