Search in sources :

Example 11 with HttpRequest

use of com.yahoo.container.jdisc.HttpRequest in project vespa by vespa-engine.

the class MultipartParserTest method multipartParserTest.

@Test
public void multipartParserTest() throws URISyntaxException {
    String data = "Content-Type: multipart/form-data; boundary=AaB03x\r\n" + "\r\n" + "--AaB03x\r\n" + "Content-Disposition: form-data; name=\"submit-name\"\r\n" + "\r\n" + "Larry\r\n" + "--AaB03x\r\n" + "Content-Disposition: form-data; name=\"submit-address\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "House 1\r\n" + "--AaB03x\r\n" + "Content-Disposition: form-data; name=\"files\"; filename=\"file1.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "... contents of file1.txt ...\r\n" + "--AaB03x--\r\n";
    ByteArrayInputStream dataStream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
    HttpRequest request = HttpRequest.createRequest(new MockCurrentContainer(), new URI("http://foo"), com.yahoo.jdisc.http.HttpRequest.Method.POST, dataStream);
    request.getJDiscRequest().headers().put("Content-Type", "multipart/form-data; boundary=AaB03x");
    Map<String, byte[]> parts = new MultipartParser().parse(request);
    assertEquals(3, parts.size());
    assertTrue(parts.containsKey("submit-name"));
    assertTrue(parts.containsKey("submit-address"));
    assertTrue(parts.containsKey("files"));
    assertEquals("Larry", new String(parts.get("submit-name"), StandardCharsets.UTF_8));
    assertEquals("... contents of file1.txt ...", new String(parts.get("files"), StandardCharsets.UTF_8));
}
Also used : HttpRequest(com.yahoo.container.jdisc.HttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) URI(java.net.URI) Test(org.junit.Test)

Example 12 with HttpRequest

use of com.yahoo.container.jdisc.HttpRequest in project vespa by vespa-engine.

the class FeedHandlerTest method testResponseIsSentAfterWaitForRequestReceivedReturns.

/**
 * nginx require that a post is finished before the server ack with a response. This behaviour is verified
 * in this test
 */
@Test
public void testResponseIsSentAfterWaitForRequestReceivedReturns() throws Exception {
    HttpRequest request = mock(HttpRequest.class);
    // Create a request with valid version.
    com.yahoo.jdisc.http.HttpRequest jdiscRequest = mock(com.yahoo.jdisc.http.HttpRequest.class);
    HeaderFields headerFields = mock(HeaderFields.class);
    List<String> version = new ArrayList<>();
    version.add("2");
    when(headerFields.get(Headers.VERSION)).thenReturn(version);
    when(jdiscRequest.headers()).thenReturn(headerFields);
    when(request.getJDiscRequest()).thenReturn(jdiscRequest);
    TestFeedHandler feedHandler = new TestFeedHandler();
    // After a short period, make the feed finish.
    new Thread(() -> {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        feedHandler.countDownLatch.countDown();
    }).start();
    // This should not return before countdown latch is stepped down.
    feedHandler.handle(request);
    // This should always returns after the countDownLatch has become zero. This might cause false positive,
    // but not false negatives. This is fine.
    assertThat(feedHandler.countDownLatch.getCount(), is(0L));
}
Also used : HttpRequest(com.yahoo.container.jdisc.HttpRequest) HeaderFields(com.yahoo.jdisc.HeaderFields) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with HttpRequest

use of com.yahoo.container.jdisc.HttpRequest in project vespa by vespa-engine.

the class V2ExternalFeedTestCase method testCleaningDoesNotBlowUp.

@Test
public final void testCleaningDoesNotBlowUp() throws IOException {
    InputStream in = new MetaStream(new byte[] { 1 });
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
    nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
    nalle.getJDiscRequest().headers().add(Headers.DRAIN, "false");
    HttpResponse r = handler.handle(nalle);
    r.render(out);
    assertEquals("", Utf8.toString(out.toByteArray()));
    handler.forceRunCleanClients();
}
Also used : HttpRequest(com.yahoo.container.jdisc.HttpRequest) InputStream(java.io.InputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 14 with HttpRequest

use of com.yahoo.container.jdisc.HttpRequest in project vespa by vespa-engine.

the class V2ExternalFeedTestCase method testSettings.

@Test
public final void testSettings() {
    HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST);
    nalle.getJDiscRequest().headers().add(Headers.DRAIN, "false");
    nalle.getJDiscRequest().headers().add(Headers.ROUTE, "bamse brakar");
    nalle.getJDiscRequest().headers().add(Headers.DENY_IF_BUSY, "true");
    FeederSettings settings = new FeederSettings(nalle);
    assertEquals(false, settings.drain);
    assertEquals(2, settings.route.getNumHops());
    assertEquals(true, settings.denyIfBusy);
}
Also used : HttpRequest(com.yahoo.container.jdisc.HttpRequest) Test(org.junit.Test)

Example 15 with HttpRequest

use of com.yahoo.container.jdisc.HttpRequest in project vespa by vespa-engine.

the class V2ExternalFeedTestCase method test.

@Test
public final void test() throws IOException, InterruptedException {
    String sessionId;
    {
        InputStream in = new MetaStream(new byte[] { 1 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "false");
        HttpResponse r = handler.handle(nalle);
        sessionId = r.headers().getFirst(Headers.SESSION_ID);
        r.render(out);
        assertEquals("", Utf8.toString(out.toByteArray()));
    }
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        assertEquals("id:banana:banana::doc1 OK Document{20}processed. \n" + "id:banana:banana::doc1 OK Document{20}processed. \n" + "id:banana:banana::doc1 OK Document{20}processed. \n", Utf8.toString(out.toByteArray()));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.US_ASCII.name(), r.getCharacterEncoding());
        assertEquals(7, logChecker.records.size());
        String actualHandshake = logChecker.records.take().getMessage();
        assertThat(actualHandshake, actualHandshake.matches("Handshake completed for client (-?)(.+?)-#(.*?)\\."), is(true));
        assertEquals("Successfully deserialized document id: id:banana:banana::doc1", logChecker.records.take().getMessage());
        assertEquals("Sent message successfully, document id: id:banana:banana::doc1", logChecker.records.take().getMessage());
    }
    // test session ID without #, i.e. something fishy related to VIPs is going on
    sessionId = "something";
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "2");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        String expectedErrorMsg = "Got request from client with id 'something', but found no session for this client.";
        assertThat(Utf8.toString(out.toByteArray()), containsString(expectedErrorMsg));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.UTF_8.name(), r.getCharacterEncoding());
    }
    // test session ID with trailing # but no hostname
    sessionId = "something#";
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        String expectedErrorMsg = "Got request from client with id 'something#', but found no session for this client.";
        assertThat(Utf8.toString(out.toByteArray()), containsString(expectedErrorMsg));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.UTF_8.name(), r.getCharacterEncoding());
    }
    // test session ID with trailing # and some unknown hostname at the end
    sessionId = "something#thisHostnameDoesNotExistAnywhere";
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        String expectedErrorMsg = "Got request from client with id 'something#thisHostnameDoesNotExistAnywhere', " + "but found no session for this client. Session was originally established " + "towards host thisHostnameDoesNotExistAnywhere, but our hostname is " + "ourHostname.";
        assertThat(Utf8.toString(out.toByteArray()), containsString(expectedErrorMsg));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.UTF_8.name(), r.getCharacterEncoding());
    }
    // test session ID with trailing # and some unknown hostname at the end
    sessionId = "something#ourHostname";
    {
        InputStream in = new MetaStream(new byte[] { 1, 3, 2 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpRequest nalle = HttpRequest.createTestRequest("http://test4-steinar:19020/reserved-for-internal-use/feedapi", Method.POST, in);
        nalle.getJDiscRequest().headers().add(Headers.VERSION, "2");
        nalle.getJDiscRequest().headers().add(Headers.TIMEOUT, "1000000000");
        nalle.getJDiscRequest().headers().add(Headers.SESSION_ID, sessionId);
        nalle.getJDiscRequest().headers().add(Headers.PRIORITY, "LOWEST");
        nalle.getJDiscRequest().headers().add(Headers.TRACE_LEVEL, "4");
        nalle.getJDiscRequest().headers().add(Headers.DRAIN, "true");
        HttpResponse r = handler.handle(nalle);
        r.render(out);
        assertEquals("id:banana:banana::doc1 OK Document{20}processed. \n" + "id:banana:banana::doc1 OK Document{20}processed. \n" + "id:banana:banana::doc1 OK Document{20}processed. \n", Utf8.toString(out.toByteArray()));
        assertEquals("text/plain", r.getContentType());
        assertEquals(StandardCharsets.US_ASCII.name(), r.getCharacterEncoding());
        Thread.sleep(1000);
    }
}
Also used : HttpRequest(com.yahoo.container.jdisc.HttpRequest) InputStream(java.io.InputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

HttpRequest (com.yahoo.container.jdisc.HttpRequest)37 Test (org.junit.Test)23 HttpResponse (com.yahoo.container.jdisc.HttpResponse)18 InputStream (java.io.InputStream)9 HandlerTest (com.yahoo.vespa.config.server.http.HandlerTest)7 SessionHandlerTest (com.yahoo.vespa.config.server.http.SessionHandlerTest)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Query (com.yahoo.search.Query)4 Joiner (com.google.common.base.Joiner)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 Inject (com.google.inject.Inject)3 Version (com.yahoo.component.Version)3 DeploymentSpec (com.yahoo.config.application.api.DeploymentSpec)3 ApplicationId (com.yahoo.config.provision.ApplicationId)3 ApplicationName (com.yahoo.config.provision.ApplicationName)3 Environment (com.yahoo.config.provision.Environment)3 RegionName (com.yahoo.config.provision.RegionName)3 TenantName (com.yahoo.config.provision.TenantName)3 LoggingRequestHandler (com.yahoo.container.jdisc.LoggingRequestHandler)3 IOUtils (com.yahoo.io.IOUtils)3