Skip to content

Commit

Permalink
#11800 add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed May 21, 2024
1 parent 2be2295 commit 7dd5c52
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@

public class AsyncContentListenerTest
{
@Test
public void testOnContentThrowingException()
{
TestSource originalSource = new TestSource(
Content.Chunk.from(ByteBuffer.wrap(new byte[] {1}), false),
Content.Chunk.from(ByteBuffer.wrap(new byte[] {2}), false),
Content.Chunk.from(ByteBuffer.wrap(new byte[] {3}), true)
);
Response.AsyncContentListener asyncContentListener = (response, chunk, demander) ->
{
throw new NumberFormatException();
};

HttpResponse response = new HttpResponse(new HttpRequest(new HttpClient(), new HttpConversation(), URI.create("http://localhost")));
asyncContentListener.onContentSource(response, originalSource);

// Assert that the source was failed.
Content.Chunk lastChunk = originalSource.read();
assertThat(Content.Chunk.isFailure(lastChunk, true), is(true));
assertThat(lastChunk.getFailure(), instanceOf(NumberFormatException.class));

// Assert that the response was aborted.
assertThat(response.getRequest().getAbortCause(), instanceOf(NumberFormatException.class));
}

@Test
public void testTransientFailureBecomesTerminal()
{
Expand Down Expand Up @@ -70,7 +95,7 @@ public void testTransientFailureBecomesTerminal()
originalSource.close();
}

private static class TestSource extends ChunksContentSource implements Closeable
static class TestSource extends ChunksContentSource implements Closeable
{
private Content.Chunk[] chunks;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.client;

import java.net.URI;
import java.nio.ByteBuffer;

import org.eclipse.jetty.client.AsyncContentListenerTest.TestSource;
import org.eclipse.jetty.client.transport.HttpConversation;
import org.eclipse.jetty.client.transport.HttpRequest;
import org.eclipse.jetty.client.transport.HttpResponse;
import org.eclipse.jetty.io.Content;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;

public class ContentListenerTest
{
@Test
public void testOnContentThrowingException()
{
TestSource originalSource = new TestSource(
Content.Chunk.from(ByteBuffer.wrap(new byte[] {1}), false),
Content.Chunk.from(ByteBuffer.wrap(new byte[] {2}), false),
Content.Chunk.from(ByteBuffer.wrap(new byte[] {3}), true)
);
Response.ContentListener contentListener = (response, content) ->
{
throw new NumberFormatException();
};

HttpResponse response = new HttpResponse(new HttpRequest(new HttpClient(), new HttpConversation(), URI.create("http://localhost")));
contentListener.onContentSource(response, originalSource);

// Assert that the source was failed.
Content.Chunk lastChunk = originalSource.read();
assertThat(Content.Chunk.isFailure(lastChunk, true), is(true));
assertThat(lastChunk.getFailure(), instanceOf(NumberFormatException.class));

// Assert that the response was aborted.
assertThat(response.getRequest().getAbortCause(), instanceOf(NumberFormatException.class));
}
}

0 comments on commit 7dd5c52

Please sign in to comment.