Skip to content

Commit

Permalink
send body if present for Micronaut client (#99)
Browse files Browse the repository at this point in the history
* send body if present for Micronaut client

* added requires for the client specs

* fixed http client factory requirements
  • Loading branch information
musketyr authored Dec 19, 2023
1 parent e4dafc5 commit 7b68098
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/heist-micronaut/src/test/groovy/heist/IdiomaticTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package heist;

import com.agorapulse.gru.Content;
import com.agorapulse.gru.Gru;
import com.agorapulse.gru.RequestDefinitionBuilder;
import io.micronaut.context.annotation.Property;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

@MicronautTest
@Property(name = "gru.http.client", value = "jdk")
public class IdiomaticTest {

@Inject
Gru gru;

@AfterEach
public void close() {
gru.close();
}

@Test
public void testHelloCommand() throws Throwable {
gru.verify(test -> test
.post("/slack/events", req -> slackEventRequest(req, "command=/hello"))
.expect(resp -> resp.json("commandHelloResponse.json"))
);
}

private static void slackEventRequest(RequestDefinitionBuilder builder, String requestBody) {
builder.header("Content-Type", "application/x-www-form-urlencoded")
.param("query", "queryValue")
.content(Content.inline(requestBody), "application/x-www-form-urlencoded");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package heist

import com.agorapulse.gru.Gru
import com.agorapulse.gru.http.Http
import io.micronaut.context.annotation.Property
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Requires
import spock.lang.Specification

@MicronautTest
@Property(name = 'gru.http.client', value = 'jdk')
@Requires({ !System.getenv('GRU_HTTP_CLIENT') || System.getenv('GRU_HTTP_CLIENT') == 'jdk' })
class JdkClientSelectionSpec extends Specification {

@Inject Gru gru

void 'client is JDK based'() {
expect:
gru.client.delegate.ensureClient() instanceof Http
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package heist

import com.agorapulse.gru.Gru
import com.agorapulse.gru.micronaut.http.MicronautHttpClient
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Requires
import spock.lang.Specification

@MicronautTest
@Requires({ !System.getenv('GRU_HTTP_CLIENT') || System.getenv('GRU_HTTP_CLIENT') == 'micronaut' })
class MicronautHttpClientSelectionSpec extends Specification {

@Inject Gru gru

void 'client is Micronaut based'() {
expect:
gru.client.delegate.ensureClient() instanceof MicronautHttpClient
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package heist

import com.agorapulse.gru.Gru
import com.agorapulse.gru.okhttp.OkHttp
import io.micronaut.context.annotation.Property
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Requires
import spock.lang.Specification

@MicronautTest
@Property(name = 'gru.http.client', value = 'okhttp')
@Requires({ !System.getenv('GRU_HTTP_CLIENT') || System.getenv('GRU_HTTP_CLIENT') == 'okhttp' })
class OkHttpClientSelectionSpec extends Specification {

@Inject Gru gru

void 'client is OkHttp based'() {
expect:
gru.client.delegate.ensureClient() instanceof OkHttp
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package heist.micronaut;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;

import java.util.Map;

@Controller("/slack")
public class SlackAppController {


@Post(value = "/events", consumes = {MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
public HttpResponse<Map<String, ?>> events(HttpRequest<String> request) {
return HttpResponse.ok(Map.of("body", request.getBody().get(), "params", request.getParameters().asMap()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"body": "command=/hello",
"params": {
"query": [
"queryValue"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@Factory
@Requires(property = GruFactory.TEST_CLASS_PROPERTY_NAME)
@Requires(classes = HttpClient.class)
@Requires(missingProperty = "gru.http.client")
@Replaces(factory = JdkClientFactory.class)
public class MicronautHttpClientFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ HttpRequest<?> buildHttpRequest() {
if (!parameters.isEmpty()) {
if (TestDefinitionBuilder.HAS_URI_PARAMETERS.contains(method) || body != null) {
builder = HttpRequest.create(HttpMethod.parse(method), buildUri(baseUri, uri, parameters).toString());
if (body != null) {
builder.body(body);
}
} else {
StringBuilder params = new StringBuilder();
appendParameters(parameters, params);
Expand Down

0 comments on commit 7b68098

Please sign in to comment.