Skip to content

Export setVars#112

Closed
zimmski wants to merge 1 commit intogorilla:masterfrom
zimmski:export-setVars
Closed

Export setVars#112
zimmski wants to merge 1 commit intogorilla:masterfrom
zimmski:export-setVars

Conversation

@zimmski
Copy link

@zimmski zimmski commented Jul 22, 2015

I need setVars for testing purposes exported. I am testing single handlers and would like to use the Vars function inside them.

@kisielk
Copy link
Contributor

kisielk commented Jul 22, 2015

I think there's other ways to test this that would be just as effective or more, without needing to expand the API of the package.

@zimmski
Copy link
Author

zimmski commented Jul 22, 2015

It seems to me that it is the easiest. Create a http.Request, add the vars to mux.SetVars and then forward it into the handler. There is actually no other mux code involved.
I am up for suggestions.
(thanks for the awesome package btw, using it for what seems ages)

@kisielk
Copy link
Contributor

kisielk commented Jul 22, 2015

The way I usually test is create a mock server using the net/http/httptest package and actual create a router instance. Then I send requests to the server which will call the appropriate handler and produce the vars I want to test. That way the routing can be tested as well.

@zimmski
Copy link
Author

zimmski commented Jul 23, 2015

Yes, I always did that too, but I noticed that testing controller handlers could be much simpler and resource friendly by simply calling them directly with a self-made http.Request instance. That way there are no unneeded server and router instances. Also then there is no need for knowing the URI. Do not get me wrong, I still test the routers. However, the dependencies of the controllers and handlers in the project are completely mockable and therefore can run perfectly fine in parallel tests. Instantiating a server and a route for every handler is just inefficient and that is the reason why I made the change in the first place.
EDIT: Also, knowing the URI is another reason to move away from the server+router test.

@arkan
Copy link

arkan commented Jul 31, 2015

+1 for this change too!

This was referenced Jul 31, 2015
@kisielk
Copy link
Contributor

kisielk commented Aug 14, 2015

@elithrar any thoughts on this?

It does add to the API a bit, but then again it doesn't add more methods to Route or Router...

My only qualm is that people may abuse it for things other than testing and that may complicate some uses of the package later...

@elithrar
Copy link
Contributor

I'd prefer to keep it private—since writing your own test helper should be a fairly simple case of calling context.Set. If you had to write a ton of boiler-plate then the argument for making it public would definitely be stronger.

@kisielk
Copy link
Contributor

kisielk commented Aug 16, 2015

Ah right, I didn't even think of that :) Right then, going to close this.

@kisielk kisielk closed this Aug 16, 2015
@zimmski
Copy link
Author

zimmski commented Aug 16, 2015

I thought so too but that does not work. The call would mimic context.Set(r, varsKey, val) and varsKey is of type contextKey. Both are not exported, so I thought I could just use the value, which is always "0". However, the key is of type interface{}, so the type of the key value becomes important. To get context.Get to work, you need a mux.contextKey(0).

@cam-stitt
Copy link

Aware that this has been closed and is an older issue, but I think @zimmski makes a pretty good point that you can't use context.Set because of the private contextKey type as well. If I am testing the handlers using httptest.NewRecorder it would be great to have one of these exported.

@bhirbec
Copy link

bhirbec commented Jun 3, 2016

I gave some thoughts about this too and I join @zimmski and @cam-stitt. My handlers are wrapped into several middlewares and using the mux/server approach would require me to write more code. As an example, my authentication middleware requires some specific HTTP headers which I could get rid of by calling the handler directly.

It's really unfortunate that there's no way to directly call the handler. I will be glad to provide a PR to open SetVar().

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

Successfully merging this pull request may close these issues.

6 participants