Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The problem with the mock #19

Closed
iliakan opened this issue Sep 13, 2014 · 13 comments
Closed

The problem with the mock #19

iliakan opened this issue Sep 13, 2014 · 13 comments

Comments

@iliakan
Copy link
Contributor

iliakan commented Sep 13, 2014

  1. There's a need for req, connection property addressed by PR Update request.js #18
  2. There's a need to keep other properties bound to req when passed by passport. Maybe a foreach loop or an additional configuration will do.

The use case: I have req.log method which logs with request information (so I know the context). I also use passReqToCallback: true in strategies to have the req object (to log it and to merge profiles).

With the new koa-passport, req.log is undefined, so the project dies.

@rkusa
Copy link
Owner

rkusa commented Sep 13, 2014

I've merged #18, thanks!

Is req.log some common used method - or very specific for your application? I think req.req.log should work (though being somehow ugly)?

@iliakan
Copy link
Contributor Author

iliakan commented Sep 13, 2014

req.log is kind of widely used method name for logging req-specific things, but it is not enforced by a framework. req.req is ugly, true, but I think that'll do for me.

Could we create a mock request as Object.create(request) or Object.create(req)? So that it naturally inherits request properties. That would solve this kind of problems.

rkusa added a commit that referenced this issue Sep 13, 2014
@rkusa
Copy link
Owner

rkusa commented Sep 13, 2014

I like your idea of using req itself as the base when creating the mock. Would you mind testing if the last commit works for you?

@iliakan
Copy link
Contributor Author

iliakan commented Sep 13, 2014

It must be req, not request, right?

@rkusa
Copy link
Owner

rkusa commented Sep 13, 2014

I could use request as the base, too, but the commit uses req.

@iliakan
Copy link
Contributor Author

iliakan commented Sep 13, 2014

It works for me.

Do I get it right that passport.js uses your mock req everywhere, including the callback (passReqToCallback: true)?

@rkusa
Copy link
Owner

rkusa commented Sep 13, 2014

Thanks for testing!

Do I get it right that passport.js uses your mock req everywhere, including the callback (passReqToCallback: true)?

Yes

@iliakan
Copy link
Contributor Author

iliakan commented Sep 13, 2014

Koa generally keeps req virgin and modifies it's own request object.

I'd suggest to inherit from request instead.

@rkusa
Copy link
Owner

rkusa commented Sep 13, 2014

I am fine with inheriting from request instead of req. But than, you've to move your log method to request.

In general, the decision is: where to expect user added properties/method. In the request or the req object.

@iliakan
Copy link
Contributor Author

iliakan commented Sep 13, 2014

I submitted the PR to turn to request.
That follows Koa conventions and is generally convenient. I changed my property to use request as well. Using req previously was actually a hack to make it work, now it would be better.

@iliakan
Copy link
Contributor Author

iliakan commented Sep 13, 2014

Please close the issue when you publish the new version, I'll replace my branch with it.

@rkusa rkusa closed this as completed in a4f507c Sep 14, 2014
@mfornasa
Copy link

@iliakan could you provide a working mocking sample?

@iliakan
Copy link
Contributor Author

iliakan commented May 18, 2015

@mfornasa What you mean?

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

No branches or pull requests

3 participants