1

在我的 Grails 应用程序中,我有一个包含部分 URL 路径的配置属性,如下所示: http://apis.mycompany.com/my-api/v1/

我想使用此 URL 调用端点HttpBuilder

class MyApiService {
    @Value('${myApiUrl}')
    String myApiUrl

    def http

    @PostConstruct()
    init() {
        http = HttpBuilder.configure {
            request.uri = myApiUrl
        }
    }

    def getData() {
        http.get() {
            // This doesn't work!!
            request.uri.path = 'resource/data'
        }
    }
}

request.uri.path擦除现有路径段:

java.lang.RuntimeException: java.net.URISyntaxException: Relative path in absolute URI: http://apis.mycompany.comresource/data

我能够解决这个问题:

request.raw = "${myApiUrl}resource/data"

...但我无法让 Ersatzraw在我的 Spock 测试中使用:

def setup() {
    ersatz = new ErsatzServer()
    service.myApiUrl = ersatz.httpUrl + '/'
}

void "getData() - should call the endpoint"() {
    given:
    def jsonData = '[{"person": "Someone"}]'

    ersatz.expectations {
        get("/resource/data") {
            called 1
            responder {
                content jsonData, 'application/json;chartset=UTF-8'
            }
        }
    }

    when:
    def result = service.getData()

    then:
    result == jsonData
}

分配给的 URLraw是:http://localhost:-1/resources/data

这会导致异常:

java.lang.IllegalArgumentException: protocol = http host = null

    at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:176)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1132)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
    at groovyx.net.http.JavaHttpBuilder$Action.lambda$execute$2(JavaHttpBuilder.java:147)
    at groovyx.net.http.JavaHttpBuilder$ThreadLocalAuth.with(JavaHttpBuilder.java:331)
    at groovyx.net.http.JavaHttpBuilder$Action.execute(JavaHttpBuilder.java:122)
    at groovyx.net.http.JavaHttpBuilder.createAndExecute(JavaHttpBuilder.java:374)
    at groovyx.net.http.JavaHttpBuilder.doGet(JavaHttpBuilder.java:381)
    at groovyx.net.http.HttpObjectConfigImpl.nullInterceptor(HttpObjectConfigImpl.java:47)
    at groovyx.net.http.HttpBuilder.get(HttpBuilder.java:346)
    at groovyx.net.http.HttpBuilder.get(HttpBuilder.java:1297)
    at com.mycompany.MyApiService.getData(...)
    at com.mycompany.MyApiServiceSpec.getData(...)

这是 HttpBuilder 中的错误,还是我做错了?帮助表示赞赏!

更新:

我以为 Ersatz 会在构建时开始,但事实证明我错了。在使用 URL 之前调用start()修复了 -1 端口号:

def setup() {
    ersatz = new ErsatzServer()
    ersatz.start()
    service.myApiUrl = ersatz.httpUrl + '/'
}

现在,构建器正在获取一个看似有效的 URL: http://localhost:52180/resources/data

...但是 Ersatz 服务器给出的消息是期望不匹配。

java.lang.AssertionError: Expectations for Expectations (ErsatzRequest): <GET>, "/resources/data" were not met.. Expression: (com.stehno.ersatz.impl.ErsatzRequest -> com.stehno.ersatz.impl.ErsatzRequest) r.verify()

    at com.stehno.ersatz.impl.ExpectationsImpl.verify_closure2(ExpectationsImpl.groovy:365)
    at com.stehno.ersatz.impl.ExpectationsImpl.verify(ExpectationsImpl.groovy:364)
    at com.stehno.ersatz.ErsatzServer.verify(ErsatzServer.groovy:522)
    at com.mycompany.MyApiServiceSpec.cleanup(...)

Ersatz 文档表明应该打印出不匹配的请求,但它只打印上面的堆栈跟踪(和 HttpNotFoundException)。

4

1 回答 1

0

对我来说,它只是附加路径而不是分配它:

request.uri.path += 'resource/data'
于 2020-11-20T10:55:54.567 回答