Skip to content

Conversation

@jmini
Copy link
Member

@jmini jmini commented Jul 16, 2019

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.
    Java: @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04)

Fixes #3370

Description of the PR

Tested with this spec:

openapi: 3.0.1
info:
  title: ping test
  version: '1.0'
servers:
  - url: 'http://localhost:8080/'
paths:
  /ping:
    get:
      operationId: pingGet
      tags:
       - lorem
      parameters:
       - in: query
         name: q
         schema:
           type: string
       - in: query
         name: r
         schema:
           type: string
      responses:
        '201':
          description: Ok

With a this test using MockServer (more on this to come in #689):

public class LoremApiTest {

    private ClientAndServer mockServer;
    private LoremApi api;

    @Before
    public void createApi() {
        mockServer = ClientAndServer.startClientAndServer();
        String port =  "" + mockServer.getLocalPort();
        StringCounter counterA = new StringCounter("a_value_", 1);
        StringCounter counterB = new StringCounter("b_value_", 1);
        api = ApiClient.api(ApiClient.Config.apiConfig().reqSpecSupplier(
                () -> new RequestSpecBuilder()
                        .setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson())))
                        .addHeader("x-header-a",  counterA.nextValue())
                        .addFilter(new ErrorLoggingFilter())
                        .setBaseUri("http://localhost:" + port ))).lorem()
                .reqSpec(r -> r.addHeader("x-header-b", counterB.nextValue()));
    }
    
    @After
    public void stopMockServer() {
        mockServer.stop();
    }

    /**
     * Ok
     */
    @Test
    public void shouldSee201AfterPingGet() {
        StringCounter counterC = new StringCounter("c_value_", 1);

        mockServer
        .when(
                HttpRequest.request()
                        .withPath("/ping")
        )
        .respond(
                HttpResponse.response()
                        .withHeader(HttpHeaderNames.CONTENT_TYPE.toString(), "application/json")
                        .withStatusCode(201)
                        .withBody("{ \"status\": \"ok\"}")
        );

        String q1 = "param1";
        api.pingGet()
            .reqSpec(r -> r.addHeader("x-header-c", counterC.nextValue()))
            .qQuery(q1).execute(r -> r.prettyPeek());
        
        String q2 = "param2";
        String r2 = "param2";
        api.pingGet()
            .reqSpec(r -> r.addHeader("x-header-c", counterC.nextValue()))
            .qQuery(q2).rQuery(r2).execute(r -> r.prettyPeek());
        
        HttpRequest[] recordedRequests = mockServer.retrieveRecordedRequests(
                HttpRequest.request()
                    .withPath("/ping")
        );
        Assert.assertEquals(2, recordedRequests.length);

        Assert.assertEquals(1, recordedRequests[0].getQueryStringParameterList().size());
        Assert.assertEquals("q", recordedRequests[0].getQueryStringParameterList().get(0).getName().getValue());
        Assert.assertEquals(1, recordedRequests[0].getQueryStringParameterList().get(0).getValues().size());
        Assert.assertEquals("param1", recordedRequests[0].getQueryStringParameterList().get(0).getValues().get(0).getValue());
        Assert.assertEquals("a_value_1", recordedRequests[0].getFirstHeader("x-header-a"));
        Assert.assertEquals("b_value_1", recordedRequests[0].getFirstHeader("x-header-b"));
        Assert.assertEquals("c_value_1", recordedRequests[0].getFirstHeader("x-header-c"));

        Assert.assertEquals(2, recordedRequests[1].getQueryStringParameterList().size());
        Assert.assertEquals("a_value_2", recordedRequests[1].getFirstHeader("x-header-a"));
        Assert.assertEquals("b_value_2", recordedRequests[1].getFirstHeader("x-header-b"));
        Assert.assertEquals("c_value_2", recordedRequests[1].getFirstHeader("x-header-c"));
        Assert.assertEquals("q", recordedRequests[1].getQueryStringParameterList().get(0).getName().getValue());
        Assert.assertEquals(1, recordedRequests[1].getQueryStringParameterList().get(0).getValues().size());
        Assert.assertEquals("param2", recordedRequests[1].getQueryStringParameterList().get(0).getValues().get(0).getValue());
    }
}

Was failing before and now it is no longer the case.

Copy link
Member

@wing328 wing328 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Java rest-assured] reqSpec is reused across multiple requests

2 participants