public String getAjaxResponseContent(final String stubType, final String propertyName) throws Exception { if (stubType.equals("request")) { return StringUtils.determineObjectStringValue( ReflectionUtils.getPropertyValue(request, propertyName)); } else if (stubType.equals("response")) { return StringUtils.determineObjectStringValue( ReflectionUtils.getPropertyValue(getResponse(), propertyName)); } else { return "Unknown stub type: " + stubType; } }
private boolean stringsMatch(final String dataStoreValue, final String thisAssertingValue) { final boolean isAssertingValueSet = StringUtils.isSet(thisAssertingValue); final boolean isDataStoreValueSet = StringUtils.isSet(dataStoreValue); if (!isDataStoreValueSet) { return true; } else if (!isAssertingValueSet) { return false; } else if (StringUtils.isWithinSquareBrackets(dataStoreValue)) { return dataStoreValue.equals(thisAssertingValue); } else { return regexMatch(dataStoreValue, thisAssertingValue); } }
public String getPostBody() { if (fileBytes.length == 0) { return FileUtils.enforceSystemLineSeparator(post); } final String utf8FileContent = StringUtils.newStringUtf8(fileBytes); return FileUtils.enforceSystemLineSeparator(utf8FileContent); }
@Override public void handle( final HttpServletRequest request, final HttpServletResponseWithGetStatus wrapper, final StubbedDataManager stubbedDataManager) throws Exception { if (!request.getRequestURI().equals(AdminHandler.ADMIN_ROOT)) { wrapper.setStatus(HttpStatus.METHOD_NOT_ALLOWED_405); wrapper.getWriter().println("Method POST is not allowed on URI " + request.getRequestURI()); return; } final String post = HandlerUtils.extractPostRequestBody(request, AdminHandler.NAME); if (!StringUtils.isSet(post)) { final String errorMessage = String.format( "%s request on URI %s was empty", request.getMethod(), request.getRequestURI()); HandlerUtils.configureErrorResponse(wrapper, HttpStatus.NO_CONTENT_204, errorMessage); return; } stubbedDataManager.refreshStubbedData(new YamlParser(), post); if (stubbedDataManager.getStubHttpLifecycles().size() == 1) { wrapper.addHeader(HttpHeaders.LOCATION, stubbedDataManager.getOnlyStubRequestUrl()); } wrapper.setStatus(HttpStatus.CREATED_201); wrapper.getWriter().println("Configuration created successfully"); }
@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); }
@Test public void shouldUnmarshallYamlIntoObjectTree_WhenYAMLValid_WithFileFailedToLoadAndBodySet() throws Exception { final String stubbedResponseFile = "../../very.big.soap.response.xml"; final String expectedBody = "{\"message\", \"Hello, this is HTTP response body\"}"; final String yaml = YAML_BUILDER .newStubbedRequest() .withMethodGet() .withUrl("/some/uri") .newStubbedResponse() .withFoldedBody(expectedBody) .withFile(stubbedResponseFile) .withStatus("201") .build(); final List<StubHttpLifecycle> loadedHttpCycles = unmarshall(yaml); final StubHttpLifecycle actualHttpLifecycle = loadedHttpCycles.get(0); final StubResponse actualResponse = actualHttpLifecycle.getResponse(true); assertThat(actualResponse.getFile()).isEqualTo(new byte[] {}); assertThat(StringUtils.newStringUtf8(actualResponse.getResponseBodyAsBytes())) .isEqualTo(expectedBody); }
public final ArrayList<String> getMethod() { final ArrayList<String> uppercase = new ArrayList<String>(method.size()); for (final String string : method) { uppercase.add(StringUtils.toUpper(string)); } return uppercase; }
public Map<String, String> getHeaders() { final Map<String, String> headersCopy = new HashMap<String, String>(headers); final Set<Map.Entry<String, String>> entrySet = headersCopy.entrySet(); this.headers.clear(); for (final Map.Entry<String, String> entry : entrySet) { this.headers.put(StringUtils.toLower(entry.getKey()), entry.getValue()); } return headers; }
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; }
public void addMethod(final String newMethod) { if (StringUtils.isSet(newMethod)) { method.add(newMethod); } }
public boolean hasPostBody() { return StringUtils.isSet(getPostBody()); }
public boolean isRestricted() { return StringUtils.isSet(getAuthorizationHeader()); }
public String getAjaxResponseContent(final String propertyName, final int sequencedResponseId) throws Exception { final List<StubResponse> allResponses = getAllResponses(); return StringUtils.determineObjectStringValue( ReflectionUtils.getPropertyValue(allResponses.get(sequencedResponseId), propertyName)); }