Skip to content

Commit

Permalink
Add test to capture error for a stream request
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jan 20, 2024
1 parent 2cc2dc8 commit 6d71228
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,24 @@ class OpenAIIntegrationTest extends OpenAIIntegrationTestBase {
void testUnauthorizedRequest() {
OpenAI unauthorizedOpenAI = OpenAI.newBuilder("foobar").build();

OpenAIException exception =
assertThrows(OpenAIException.class, () -> unauthorizedOpenAI.modelsClient().listModels());
ModelsClient modelsClient = unauthorizedOpenAI.modelsClient();

OpenAIException exception = assertThrows(OpenAIException.class, modelsClient::listModels);

assertThat(exception.statusCode()).isEqualTo(401);
assertThat(exception.errorMessage()).startsWith("Incorrect API key provided: foobar");

// test capturing exception when stream requested
CreateChatCompletionRequest streamRequest =
CreateChatCompletionRequest.newBuilder()
.message(ChatMessage.userMessage("Who won the world series in 2020?"))
.stream(true)
.build();

ChatClient chatClient = unauthorizedOpenAI.chatClient();

exception =
assertThrows(OpenAIException.class, () -> chatClient.streamChatCompletion(streamRequest));

assertThat(exception.statusCode()).isEqualTo(401);
assertThat(exception.errorMessage()).startsWith("Incorrect API key provided: foobar");
Expand Down

0 comments on commit 6d71228

Please sign in to comment.