Beispiel #1
0
  @Test
  public void shouldUnmarshallYamlIntoObjectTree_WhenYAMLValid_WithEmptyAuthorizationHeader()
      throws Exception {

    final String authorization = "";

    final String yaml =
        YAML_BUILDER
            .newStubbedRequest()
            .withMethodGet()
            .withUrl("/some/uri")
            .withHeaderAuthorization("")
            .newStubbedResponse()
            .withStatus("301")
            .build();

    final List<StubHttpLifecycle> loadedHttpCycles = unmarshall(yaml);
    final StubHttpLifecycle actualHttpLifecycle = loadedHttpCycles.get(0);
    final StubRequest actualRequest = actualHttpLifecycle.getRequest();

    final String encodedAuthorizationHeader =
        String.format("%s %s", "Basic", StringUtils.encodeBase64(authorization));
    final MapEntry headerOneEntry = MapEntry.entry("authorization", encodedAuthorizationHeader);

    assertThat(actualRequest.getHeaders()).contains(headerOneEntry);
  }
Beispiel #2
0
  @Test
  public void
      shouldUnmarshallYamlIntoObjectTree_WhenYAMLValid_WitQueryParamIsArrayHavingSingleQuotes()
          throws Exception {

    final String expectedParamOne = "fruits";
    final String expectedParamOneValue = "['apple','orange','banana']";
    final String fullQueryOne = String.format("%s=%s", expectedParamOne, expectedParamOneValue);

    final String yaml =
        YAML_BUILDER
            .newStubbedRequest()
            .withMethodGet()
            .withUrl("/some/uri")
            .withQuery(expectedParamOne, String.format("\"%s\"", expectedParamOneValue))
            .newStubbedResponse()
            .withStatus("201")
            .build();

    final List<StubHttpLifecycle> loadedHttpCycles = unmarshall(yaml);
    final StubHttpLifecycle actualHttpLifecycle = loadedHttpCycles.get(0);
    final StubRequest actualRequest = actualHttpLifecycle.getRequest();
    final MapEntry queryEntryOne = MapEntry.entry(expectedParamOne, expectedParamOneValue);

    assertThat(actualRequest.getUrl()).contains(fullQueryOne);
    assertThat(actualRequest.getQuery()).contains(queryEntryOne);
  }
Beispiel #3
0
  @Test
  public void shouldUnmarshallYamlIntoObjectTree_WhenYAMLValid_WithMultipleQueryParams()
      throws Exception {

    final String expectedParamOne = "paramOne";
    final String expectedParamOneValue = "one";
    final String fullQueryOne = String.format("%s=%s", expectedParamOne, expectedParamOneValue);

    final String expectedParamTwo = "paramTwo";
    final String expectedParamTwoValue = "two";
    final String fullQueryTwo = String.format("%s=%s", expectedParamTwo, expectedParamTwoValue);

    final String yaml =
        YAML_BUILDER
            .newStubbedRequest()
            .withMethodGet()
            .withUrl("/some/uri")
            .withQuery(expectedParamOne, expectedParamOneValue)
            .withQuery(expectedParamTwo, expectedParamTwoValue)
            .newStubbedResponse()
            .withStatus("500")
            .build();

    final List<StubHttpLifecycle> loadedHttpCycles = unmarshall(yaml);
    final StubHttpLifecycle actualHttpLifecycle = loadedHttpCycles.get(0);
    final StubRequest actualRequest = actualHttpLifecycle.getRequest();
    final MapEntry queryEntryOne = MapEntry.entry(expectedParamOne, expectedParamOneValue);
    final MapEntry queryEntryTwo = MapEntry.entry(expectedParamTwo, expectedParamTwoValue);

    assertThat(actualRequest.getUrl()).contains(fullQueryOne);
    assertThat(actualRequest.getUrl()).contains(fullQueryTwo);
    assertThat(actualRequest.getQuery()).contains(queryEntryOne, queryEntryTwo);
  }
Beispiel #4
0
  @Test
  public void shouldUnmarshallYamlIntoObjectTree_WhenYAMLValid_WithFileFailedToLoadAndPostSet()
      throws Exception {

    final String stubbedRequestFile = "../../very.big.soap.request.xml";

    final String expectedPost = "{\"message\", \"Hello, this is HTTP request post\"}";
    final String yaml =
        YAML_BUILDER
            .newStubbedRequest()
            .withMethodGet()
            .withUrl("/some/uri")
            .withFile(stubbedRequestFile)
            .withFoldedPost(expectedPost)
            .newStubbedResponse()
            .withLiteralBody("OK")
            .withStatus("201")
            .build();

    final List<StubHttpLifecycle> loadedHttpCycles = unmarshall(yaml);

    final StubHttpLifecycle actualHttpLifecycle = loadedHttpCycles.get(0);
    final StubRequest actualRequest = actualHttpLifecycle.getRequest();

    assertThat(actualRequest.getFile()).isEqualTo(new byte[] {});
    assertThat(actualRequest.getPostBody()).isEqualTo(expectedPost);
  }
Beispiel #5
0
  @Test
  public void shouldUnmarshallYamlIntoObjectTree_WhenYAMLValid_WithMultipleHTTPMethods()
      throws Exception {

    final String yaml =
        YAML_BUILDER
            .newStubbedRequest()
            .withMethodGet()
            .withMethodHead()
            .newStubbedResponse()
            .withStatus("301")
            .build();

    final List<StubHttpLifecycle> loadedHttpCycles = unmarshall(yaml);
    final StubHttpLifecycle actualHttpLifecycle = loadedHttpCycles.get(0);
    final StubRequest actualRequest = actualHttpLifecycle.getRequest();

    assertThat(actualRequest.getMethod()).contains(HttpMethods.GET, HttpMethods.HEAD);
  }
Beispiel #6
0
  @Test
  public void shouldUnmarshallYamlIntoObjectTree_WhenYAMLValid_WithUrlAsRegex() throws Exception {

    final String url = "^/[a-z]{3}/[0-9]+/?$";
    final String yaml =
        YAML_BUILDER
            .newStubbedRequest()
            .withMethodGet()
            .withUrl(url)
            .newStubbedResponse()
            .withStatus("301")
            .build();

    final List<StubHttpLifecycle> loadedHttpCycles = unmarshall(yaml);
    final StubHttpLifecycle actualHttpLifecycle = loadedHttpCycles.get(0);
    final StubRequest actualRequest = actualHttpLifecycle.getRequest();

    assertThat(actualRequest.getUrl()).isEqualTo(url);
  }
Beispiel #7
0
  public static StubRequest createFromHttpServletRequest(final HttpServletRequest request)
      throws IOException {
    final StubRequest assertionRequest =
        StubRequest.newStubRequest(
            request.getPathInfo(), HandlerUtils.extractPostRequestBody(request, "stubs"));
    assertionRequest.addMethod(request.getMethod());

    final Enumeration<String> headerNamesEnumeration = request.getHeaderNames();
    final List<String> headerNames =
        ObjectUtils.isNotNull(headerNamesEnumeration)
            ? Collections.list(request.getHeaderNames())
            : new LinkedList<String>();
    for (final String headerName : headerNames) {
      final String headerValue = request.getHeader(headerName);
      assertionRequest.getHeaders().put(StringUtils.toLower(headerName), headerValue);
    }

    assertionRequest.getQuery().putAll(CollectionUtils.constructParamMap(request.getQueryString()));

    return assertionRequest;
  }
Beispiel #8
0
  @Test
  public void shouldUnmarshallYamlIntoObjectTree_WhenYAMLValid_WithLiteralPost() throws Exception {

    final String stubbedRequestPost = "Hello, this is a request post";

    final String yaml =
        YAML_BUILDER
            .newStubbedRequest()
            .withMethodGet()
            .withUrl("/some/uri")
            .withLiteralPost(stubbedRequestPost)
            .newStubbedResponse()
            .withStatus("201")
            .build();

    final List<StubHttpLifecycle> loadedHttpCycles = unmarshall(yaml);
    final StubHttpLifecycle actualHttpLifecycle = loadedHttpCycles.get(0);
    final StubRequest actualRequest = actualHttpLifecycle.getRequest();

    assertThat(actualRequest.getPost()).isEqualTo(stubbedRequestPost);
  }
  @Override
  public boolean equals(final Object o) {
    if (this == o) {
      return true;
    }

    if (!(o instanceof StubHttpLifecycle)) {
      return false;
    }

    final StubHttpLifecycle that = (StubHttpLifecycle) o;
    if (!request.equals(that.request)) {
      return false;
    }

    return true;
  }
Beispiel #10
0
  @Override
  public boolean equals(final Object o) {
    if (this == o) {
      return true;
    } else if (o instanceof StubRequest) {
      final StubRequest dataStoreRequest = (StubRequest) o;

      if (!urlsMatch(dataStoreRequest.url, url)) {
        return false;
      }
      if (!arraysIntersect(dataStoreRequest.getMethod(), getMethod())) {
        ANSITerminal.dump(
            String.format(
                "METHOD %s failed match for %s", dataStoreRequest.getMethod(), getMethod()));
        return false;
      }
      if (!postBodiesMatch(dataStoreRequest.getPostBody(), getPostBody())) {
        ANSITerminal.dump(
            String.format(
                "POST BODY %s failed match for %s", dataStoreRequest.getPostBody(), getPostBody()));
        return false;
      }
      if (!headersMatch(dataStoreRequest.getHeaders(), getHeaders())) {
        ANSITerminal.dump(
            String.format(
                "HEADERS %s failed match for %s", dataStoreRequest.getHeaders(), getHeaders()));
        return false;
      }
      if (!queriesMatch(dataStoreRequest.getQuery(), getQuery())) {
        ANSITerminal.dump(
            String.format("QUERY %s failed match for %s", dataStoreRequest.getQuery(), getQuery()));
        return false;
      }
      return true;
    }

    return false;
  }
 private String getAuthorizationHeader() {
   return request.getHeaders().get(StubRequest.AUTH_HEADER);
 }