【问题标题】:Grails 2.5.0 - integration testing with spring security for RESTful serviceGrails 2.5.0 - RESTful 服务的 Spring Security 集成测试
【发布时间】:2016-01-06 16:25:13
【问题描述】:

我想测试 Grails Web 应用程序的安全性,确保只有某些角色可以 POST 并给出正确的响应。我在集成测试中有以下内容:

class MyIntegrationSpec extends IntegrationSpec {
    def grailsApplication
    def testdataService

    def setup() {
        testdataService.addTestData()
        SpringSecurityUtils.reauthenticate 'user1', null
    }

    def cleanup() {
    }

    void "test case"() {
        given:
        RestBuilder rest = new RestBuilder()

        when:
        RestResponse response = rest.post("http://localhost:8080/${grailsApplication.metadata.'app.name'}/mycontroller/myaction") {
            json([
                    data1: "foo",
                    data2: "bar"
            ])
        }

        then:
        response.status == 200
    }
}

不幸的是,集成测试在 Spring Security 启动之前运行,我收到以下错误:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8080/myapp/mycontroller/myaction":Connection refused: connect; nested exception is java.net.ConnectException

如何让我的集成测试运行,以便测试用户体验,尤其是安全性?

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    没有为集成测试启动 Web 服务器。 Spring 应用程序上下文已启动,插件处于活动状态,有一个可用的真实数据库(默认情况下在内存中,但您可以将其配置为指向服务器)等等,但没有 Web 服务器。

    如果您想测试需要运行 Web 服务器的安全性和其他功能,则需要使用功能测试。我用过http://grails.org/plugin/functional-test,它非常好,但有些错误。我使用的是 1.2.7 版本,但我发现现在有 2.0 版本,所以可能会更好。

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 2018-11-18
      • 2023-03-30
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多