CurlBuilder { public String build() { List<String> parts = new ArrayList<>(); parts.add("curl"); parts.addAll(options); parts.add(String.format(FORMAT_METHOD, method.toUpperCase())); for (Header header : headers) { final String headerPart = String.format(FORMAT_HEADER, header.name(), header.value()); parts.add(headerPart); } if (contentType != null && !containsName(CONTENT_TYPE, headers)) { parts.add(String.format(FORMAT_HEADER, CONTENT_TYPE, contentType)); } if (body != null) { parts.add(String.format(FORMAT_BODY, body)); } parts.add(String.format(FORMAT_URL, url)); return join(delimiter, parts); } CurlBuilder(Request request);  CurlBuilder(Request request, long limit, List<HeaderModifier> headerModifiers, Options options);  CurlBuilder(Request request, long limit, List<HeaderModifier> headerModifiers, Options options, String delimiter); String build();  }
@Test public void getRequestContainsConnectTimeoutOptions() { final Request request = new Request.Builder().url("http: final Options options = Options.builder().connectTimeout(120).build(); final String command = new CurlBuilder(request, 1024, Collections.<HeaderModifier>emptyList(), options).build(); assertEquals("curl --connect-timeout 120 -X GET \"http: }
@Test public void getRequesWithDelimiter() { final Request request = new Request.Builder().url("http: final String command = new CurlBuilder(request, -1L, Collections.emptyList(), Options.EMPTY, " \\\n").build(); assertEquals("curl \\\n-X GET \\\n\"http: }
@Test public void getRequestHasCorrectCommand() { final Request request = new Request.Builder().url("http: final String command = new CurlBuilder(request).build(); assertEquals("curl -X GET \"http: }
@Test public void getRequestHasCorrectHeader() { final Request request = new Request.Builder() .url("http: .header("Accept", "application/json") .build(); final String command = new CurlBuilder(request).build(); assertEquals("curl -X GET -H \"Accept:application/json\" \"http: }
@Test public void getRequestHasCorrectCacheHeader() { final CacheControl cache = new CacheControl.Builder() .maxAge(1, TimeUnit.DAYS) .onlyIfCached() .build(); final Request request = new Request.Builder() .url("http: .cacheControl(cache) .build(); final String command = new CurlBuilder(request).build(); assertEquals("curl -X GET -H \"Cache-Control:max-age=86400, only-if-cached\" \"http: }
@Test public void postRequestHasCorrectPostData() { final Request request = new Request.Builder().url("http: final String command = new CurlBuilder(request).build(); final String expected = "curl -X POST -H \"Content-Type:application/x-www-form-urlencoded\" -d 'key1=value1' \"http: assertEquals(expected, command); }
@Test public void postRequestBodyWithNullMediaType() { final RequestBody body = RequestBody.create(null, "StringBody"); final Request request = new Request.Builder().url("http: final String command = new CurlBuilder(request).build(); final String expected = "curl -X POST -d 'StringBody' \"http: assertEquals(expected, command); }
@Test public void multipleHeadersWithTheSameNameShouldBeAddedToCurlCommand() { final Request request = new Request.Builder() .url("http: .addHeader("Cookie", "FIRST=foo") .addHeader("Cookie", "SECOND=bar") .build(); final String command = new CurlBuilder(request).build(); assertEquals("curl -X GET -H \"Cookie:FIRST=foo\" -H \"Cookie:SECOND=bar\" \"http: }
@Test public void getRequestContainsInsecureOption() { final Request request = new Request.Builder().url("http: final Options options = Options.builder().insecure().build(); final String command = new CurlBuilder(request, 1024, Collections.emptyList(), options).build(); assertEquals("curl --insecure -X GET \"http: }