如何将HttpBuilder与URL中的部分路径一起使用?

时间:2019-05-07 14:32:50

标签: spock grails-3.3 httpbuilder-ng ersatz

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

我想使用带有HttpBuilder的URL来调用端点:

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"

...但是在我的Spock测试中,我无法让Ersatz与raw一起工作:

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
}

分配给raw的URL是: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)。

1 个答案:

答案 0 :(得分:0)

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

request.uri.path += 'resource/data'