Example usage for io.netty.handler.codec.http HttpHeaderValues TEXT_PLAIN

List of usage examples for io.netty.handler.codec.http HttpHeaderValues TEXT_PLAIN

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaderValues TEXT_PLAIN.

Prototype

AsciiString TEXT_PLAIN

To view the source code for io.netty.handler.codec.http HttpHeaderValues TEXT_PLAIN.

Click Source Link

Document

"text/plain"

Usage

From source file:org.ballerinalang.test.service.http.sample.ExpectContinueTestCase.java

License:Open Source License

@Test(description = "Test 100 continue response and for request with expect:100-continue header")
public void test100Continue() {
    HttpClient httpClient = new HttpClient("localhost", servicePort);

    DefaultHttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/continue");
    DefaultLastHttpContent reqPayload = new DefaultLastHttpContent(
            Unpooled.wrappedBuffer(TestUtils.LARGE_ENTITY.getBytes()));

    httpRequest.headers().set(HttpHeaderNames.CONTENT_LENGTH, TestUtils.LARGE_ENTITY.getBytes().length);
    httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);
    httpRequest.headers().set("X-Status", "Positive");

    List<FullHttpResponse> responses = httpClient.sendExpectContinueRequest(httpRequest, reqPayload);

    Assert.assertFalse(httpClient.waitForChannelClose());

    // 100-continue response
    Assert.assertEquals(responses.get(0).status(), HttpResponseStatus.CONTINUE);
    Assert.assertEquals(Integer.parseInt(responses.get(0).headers().get(HttpHeaderNames.CONTENT_LENGTH)), 0);

    // Actual response
    String responsePayload = TestUtils.getEntityBodyFrom(responses.get(1));
    Assert.assertEquals(responses.get(1).status(), HttpResponseStatus.OK);
    Assert.assertEquals(responsePayload, TestUtils.LARGE_ENTITY);
    Assert.assertEquals(responsePayload.getBytes().length, TestUtils.LARGE_ENTITY.getBytes().length);
    Assert.assertEquals(Integer.parseInt(responses.get(1).headers().get(HttpHeaderNames.CONTENT_LENGTH)),
            TestUtils.LARGE_ENTITY.getBytes().length);
}

From source file:org.ballerinalang.test.service.http.sample.ExpectContinueTestCase.java

License:Open Source License

@Test(description = "Test ignoring inbound payload with a 417 response for request with expect:100-continue header")
public void test100ContinueNegative() {
    HttpClient httpClient = new HttpClient("localhost", servicePort);

    DefaultHttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/continue");
    DefaultLastHttpContent reqPayload = new DefaultLastHttpContent(
            Unpooled.wrappedBuffer(TestUtils.LARGE_ENTITY.getBytes()));

    httpRequest.headers().set(HttpHeaderNames.CONTENT_LENGTH, TestUtils.LARGE_ENTITY.getBytes().length);
    httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);

    List<FullHttpResponse> responses = httpClient.sendExpectContinueRequest(httpRequest, reqPayload);

    Assert.assertFalse(httpClient.waitForChannelClose());

    // 417 Expectation Failed response
    Assert.assertEquals(responses.get(0).status(), HttpResponseStatus.EXPECTATION_FAILED,
            "Response code mismatch");
    int length = Integer.parseInt(responses.get(0).headers().get(HttpHeaderNames.CONTENT_LENGTH));
    Assert.assertEquals(length, 26, "Content length mismatched");
    String payload = responses.get(0).content().readCharSequence(length, Charset.defaultCharset()).toString();
    Assert.assertEquals(payload, "Do not send me any payload", "Entity body mismatched");
    // Actual response
    Assert.assertEquals(responses.size(), 1,
            "Multiple responses received when only a 417 response was expected");
}

From source file:org.ballerinalang.test.service.http.sample.ExpectContinueTestCase.java

License:Open Source License

@Test
public void test100ContinuePassthrough() {
    HttpClient httpClient = new HttpClient("localhost", servicePort);

    DefaultHttpRequest reqHeaders = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/continue/testPassthrough");
    DefaultLastHttpContent reqPayload = new DefaultLastHttpContent(
            Unpooled.wrappedBuffer(TestUtils.LARGE_ENTITY.getBytes()));

    reqHeaders.headers().set(HttpHeaderNames.CONTENT_LENGTH, TestUtils.LARGE_ENTITY.getBytes().length);
    reqHeaders.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);

    List<FullHttpResponse> responses = httpClient.sendExpectContinueRequest(reqHeaders, reqPayload);

    Assert.assertFalse(httpClient.waitForChannelClose());

    // 100-continue response
    Assert.assertEquals(responses.get(0).status(), HttpResponseStatus.CONTINUE);
    Assert.assertEquals(Integer.parseInt(responses.get(0).headers().get(HttpHeaderNames.CONTENT_LENGTH)), 0);

    // Actual response
    String responsePayload = TestUtils.getEntityBodyFrom(responses.get(1));
    Assert.assertEquals(responses.get(1).status(), HttpResponseStatus.OK);
    Assert.assertEquals(responsePayload, TestUtils.LARGE_ENTITY);
    Assert.assertEquals(responsePayload.getBytes().length, TestUtils.LARGE_ENTITY.getBytes().length);
    Assert.assertEquals(Integer.parseInt(responses.get(1).headers().get(HttpHeaderNames.CONTENT_LENGTH)),
            TestUtils.LARGE_ENTITY.getBytes().length);
}

From source file:org.ballerinalang.test.util.HttpClientRequest.java

License:Open Source License

/**
 * Sends multipart/form-data requests./*from  w  w  w.  ja v a2 s.  co m*/
 *
 * @param requestUrl - The URL of the service
 * @param headers    - http request header map
 * @param formData   - map of form data
 * @return - HttpResponse from the end point
 * @throws IOException If an error occurs while sending the GET request
 */
public static HttpResponse doMultipartFormData(String requestUrl, Map<String, String> headers,
        Map<String, String> formData) throws IOException {
    String boundary = "---" + System.currentTimeMillis() + "---";
    String lineFeed = "\r\n";
    HttpURLConnection urlConnection = null;

    try {
        urlConnection = getURLConnection(requestUrl);
        setHeadersAndMethod(urlConnection, headers, TestConstant.HTTP_METHOD_POST);
        urlConnection.setUseCaches(false);
        urlConnection.setDoInput(true);
        urlConnection.setRequestProperty(HttpHeaderNames.CONTENT_TYPE.toString(),
                HttpHeaderValues.MULTIPART_FORM_DATA + "; boundary=" + boundary);

        try (OutputStream out = urlConnection.getOutputStream()) {
            Writer writer = new OutputStreamWriter(out, TestConstant.CHARSET_NAME);
            for (Map.Entry<String, String> data : formData.entrySet()) {
                writer.append("--" + boundary).append(lineFeed);
                writer.append("Content-Disposition: form-data; name=\"" + data.getKey() + "\"")
                        .append(lineFeed);
                writer.append(HttpHeaderNames.CONTENT_TYPE.toString() + ":" + HttpHeaderValues.TEXT_PLAIN
                        + "; charset=" + TestConstant.CHARSET_NAME).append(lineFeed);
                writer.append(lineFeed);
                writer.append(data.getValue()).append(lineFeed);
                writer.flush();
            }
            writer.append(lineFeed).flush();
            writer.append("--" + boundary + "--").append(lineFeed);
            writer.close();
        }
        return buildResponse(urlConnection);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}