private void assertCodeMakes( String method, URI uri, int statusCode, String message, String contentType, String content, Class<? extends Exception> expected) { ElasticStackErrorHandler function = Guice.createInjector().getInstance(ElasticStackErrorHandler.class); HttpCommand command = createMock(HttpCommand.class); HttpRequest request = new HttpRequest(method, uri); HttpResponse response = new HttpResponse( statusCode, message, Payloads.newInputStreamPayload(Strings2.toInputStream(content))); response.getPayload().getContentMetadata().setContentType(contentType); expect(command.getCurrentRequest()).andReturn(request).atLeastOnce(); command.setException(classEq(expected)); replay(command); function.handleError(command, response); verify(command); }
@Test(groups = {"integration", "live"}) public void testPut() throws IOException, InterruptedException { String bucketName = getContainerName(); try { Map<String, Blob> map = createMap(context, bucketName); Blob blob = context.getBlobStore().newBlob("one"); blob.setPayload(Strings2.toInputStream("apple")); Payloads.calculateMD5(blob); Blob old = map.put(blob.getMetadata().getName(), blob); getOneReturnsAppleAndOldValueIsNull(map, old); blob.setPayload(Strings2.toInputStream("bear")); Payloads.calculateMD5(blob); Blob apple = map.put(blob.getMetadata().getName(), blob); getOneReturnsBearAndOldValueIsApple(map, apple); } finally { returnContainer(bucketName); } }
@VisibleForTesting HttpRequest calculateAndReplaceAuthorizationHeaders(HttpRequest request, String toSign) throws HttpException { String signature = sign(toSign); if (signatureWire.enabled()) signatureWire.input(Strings2.toInputStream(signature)); String[] signatureLines = Iterables.toArray(Splitter.fixedLength(60).split(signature), String.class); Multimap<String, String> headers = ArrayListMultimap.create(); for (int i = 0; i < signatureLines.length; i++) { headers.put("X-Ops-Authorization-" + (i + 1), signatureLines[i]); } return request.toBuilder().replaceHeaders(headers).build(); }
@Test public void testResponseOk() throws Exception { Function<HttpResponse, URI> function = new ParseURIFromListOrLocationHeaderIf20x(uriBuilderProvider); HttpResponse response = createMock(HttpResponse.class); Payload payload = createMock(Payload.class); expect(response.getStatusCode()).andReturn(200).atLeastOnce(); expect(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE)).andReturn("text/uri-list"); expect(response.getPayload()).andReturn(payload).atLeastOnce(); expect(payload.getInput()).andReturn(Strings2.toInputStream("http://locahost")).atLeastOnce(); payload.release(); replay(payload); replay(response); assertEquals(function.apply(response), URI.create("http://locahost")); verify(payload); verify(response); }
private void assertCodeMakes( String method, URI uri, int statusCode, String message, String contentType, String content, Class<? extends Exception> expected) { ParseAzureBlobErrorFromXmlContent function = Guice.createInjector( new SaxParserModule(), new AbstractModule() { @Override protected void configure() { bind(SharedKeyLiteAuthentication.class) .toInstance(createMock(SharedKeyLiteAuthentication.class)); } }) .getInstance(ParseAzureBlobErrorFromXmlContent.class); HttpCommand command = createMock(HttpCommand.class); HttpRequest request = new HttpRequest(method, uri); HttpResponse response = new HttpResponse( statusCode, message, Payloads.newInputStreamPayload(Strings2.toInputStream(content))); response.getPayload().getContentMetadata().setContentType(contentType); expect(command.getCurrentRequest()).andReturn(request).atLeastOnce(); command.setException(classEq(expected)); replay(command); function.handleError(command, response); verify(command); }