简体   繁体   English

您如何对oauth的控制器进行单元测试?

[英]How do you unit-test controllers that oauth?

I like Spring MVC because you can unit test your controllers. 我喜欢Spring MVC,因为您可以对控制器进行单元测试。

But testing controllers that oauth is another thing. 但是对控制器进行oauth测试是另一回事。 For instance if I want to get the authorization url because I want to Oauth to GData, I would have to deploy the web-app because Google will only accept authorization requests from my domain (the url of my web app), not my development environment whose domain is localhost:8080. 例如,如果我想因为Oauth到GData而获得授权URL,则必须部署Web应用程序,因为Google仅接受来自我的域(Web应用程序的URL)的授权请求,而不是我的开发环境其域为localhost:8080。

So right now the only way I am testing if my code works is deploying the code and printing out the data that I need to have printed. 因此,现在测试代码是否正常的唯一方法是部署代码并打印出我需要打印的数据。

My Controller, which is a multi-action controller 我的控制器,这是一个多动作控制器

public ModelAndView authorize(HttpServletRequest request,
HttpServletResponse response) {

        Provider provider = getProvider(request.getAttribute("provider"));
        String authUrl = provider.getAuthUrl();     
        page.put("authUrl", authUrl);
        return new ModelAndView("setup","model",page);
}

The provider code, all my dependencies are injected 提供程序代码,我所有的依赖项都已注入

public String getAuthUrl()
{
    oAuthParameters.setScope("http://docs.google.com/feeds/");          
    try {
       oAuthHelper.getUnauthorizedRequestToken(oAuthParameters);
    } catch (OAuthException e) {
        page.put("authUrl", CANNOT_CONNECT_TO_GOOGLE);
    }
    String oAuth_Callback="[callback url]";
    try {
        oAuth_Callback.concat("?oauth_token_secret=").concat(
            java.net.URLEncoder.encode
           (oAuthParameters.getOAuthTokenSecret(), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        page.put("authUrl",INTERNAL_ERROR);
    }

    oAuthParameters.setOAuthCallback(oAuth_Callback);
    String authUrl = oAuthHelper.createUserAuthorizationUrl(oAuthParameters);
    return authUrl;
}

It sounds like you have one component (a controller) doing multiple things. 听起来您有一个组件(一个控制器)可以做很多事情。

I would break this into 我会分解成

  1. The controller 控制器
  2. The OAuth service that communicates with Google 与Google通信的OAuth服务

The latter should be injected into your controller, as with just about everything else in Spring. 后者应该注入到您的控制器中,就像Spring中的其他所有东西一样。

This allows you, in a unit test, to mock out how your controller behaves when the OAuth component returns different values. 这样,在单元测试中,您可以模拟OAuth组件返回不同值时控制器的行为。

For actually testing integration with Google, you could do two things: 要实际测试与Google的集成,您可以做两件事:

  1. Unit testing of the service that parses the Google OAuth response - mock out the code that does the actual message transport so that you can test how your message parser behaves when google returns a certain type of XML (I'm assuming this is done with XML, but same principle applies regardless of technology) vs another type. 解析Google OAuth响应的服务的单元测试-模拟执行实际消息传输的代码,以便您可以测试当Google返回某种类型的XML时消息解析器的行为(我假设这是通过XML完成的,但无论采用哪种技术,都适用相同的原则)。
  2. Actual integration tests of the component that sends and receives to google - this might be harder because of the limitations you mentioned. 发送和接收到Google的组件的实际集成测试-由于您提到的限制,这可能会更难。

So, even if they restrict access to certain domains, then you can unit test most of the pieces of the puzzle, and hopefully only have one small segment that has to be "in the wild" to be tested. 因此,即使它们限制了对某些域的访问,也可以对大部分难题进行单元测试,并希望只有一小部分必须“在野外”进行测试。

Or, could you register a different account for a domain in your test environment? 或者,您可以在测试环境中为域注册其他帐户吗? Either way, you should still break up this code into smaller components. 无论哪种方式,您都仍应将此代码分解为较小的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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