简体   繁体   中英

Is it possible to use WebMock with Rack Test?

Using WebMock to stub an Oauth 2 Provider. The issue is that I want to use Rack Test.

Rack Test only runs against an instance of the app, and does not know about external HTTP hosts like the Oauth 2 Provider.

WebMock.stub_request(:get, "https://test.oauth-provider.com/oauth/authorize") won't work because the request is sent to the app as /oauth/authorize .

Is there a way for WebMock to respond to local requests? For example:

WebMock.stub_request(:get, "/oauth/authorize")

It seems to me, you're a little bit confused about what you're testing, and must draw the clear border, where your system is (so-called SuT, system under test) and where are the external parties.

It is very important since:

  • SuT is what you're going to interact with during your tests (ie your Rack application);
  • external parties are to be mocked (ie external web services, and... which may be surprising, other libraries, like OAuth, SQL drivers etc.)

That means that if you're trying to use WebMock for any part of your application (which it looks like what you're doing due to the question about mocking a relative URL), you're clearly doing something wrong.

Closer to your task, if I were you, I would:

  1. Pick a good, well-tested OAuth library and drop it into the application.
  2. When it comes to testing, just use my own simple stub objects instead of the real OAuth implementation classes. This will shift the focus on testing the behavior my service implements. Verifying OAuth library is really a double work, since it's already covered by its authors.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM