|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.genai; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
| 23 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | +import com.google.genai.types.Candidate; |
| 25 | +import com.google.genai.types.GenerateContentResponse; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.util.Iterator; |
| 28 | +import okhttp3.Headers; |
| 29 | +import okhttp3.MediaType; |
| 30 | +import okhttp3.ResponseBody; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +public final class ResponseStreamTest { |
| 34 | + |
| 35 | + public static class DummyConverter { |
| 36 | + public JsonNode convert(JsonNode fromObject, ObjectNode parentObject) { |
| 37 | + return fromObject; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testMultiLineSseParsing() throws Exception { |
| 43 | + String sseData = |
| 44 | + "data: {\"candidates\": [{\"content\": {\"parts\": [{\"text\": \"line1\\n\"}]}},\n" |
| 45 | + + "data: {\"content\": {\"parts\": [{\"text\": \"line2\"}]}}]}\n" |
| 46 | + + "\n"; // End of event |
| 47 | + |
| 48 | + ResponseBody body = |
| 49 | + ResponseBody.create( |
| 50 | + sseData.getBytes(StandardCharsets.UTF_8), MediaType.parse("text/event-stream")); |
| 51 | + FakeApiResponse response = new FakeApiResponse(Headers.of(), body); |
| 52 | + |
| 53 | + DummyConverter converter = new DummyConverter(); |
| 54 | + ResponseStream<GenerateContentResponse> responseStream = |
| 55 | + new ResponseStream<>(GenerateContentResponse.class, response, converter, "convert"); |
| 56 | + |
| 57 | + Iterator<GenerateContentResponse> iterator = responseStream.iterator(); |
| 58 | + |
| 59 | + assertTrue(iterator.hasNext()); |
| 60 | + GenerateContentResponse response1 = iterator.next(); |
| 61 | + |
| 62 | + assertTrue(response1.candidates().isPresent()); |
| 63 | + assertEquals(2, response1.candidates().get().size()); |
| 64 | + |
| 65 | + Candidate c1 = response1.candidates().get().get(0); |
| 66 | + assertEquals("line1\n", c1.content().get().text()); |
| 67 | + |
| 68 | + Candidate c2 = response1.candidates().get().get(1); |
| 69 | + assertEquals("line2", c2.content().get().text()); |
| 70 | + |
| 71 | + assertTrue(!iterator.hasNext()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testIgnoreNonSseLines() throws Exception { |
| 76 | + String sseData = |
| 77 | + "data: {\"candidates\": [{\"content\": {\"parts\": [{\"text\": \"valid data\"}]}}]}\n" |
| 78 | + + ": some comment line\n" |
| 79 | + + "ignored field: some value\n" |
| 80 | + + "\n"; // End of event |
| 81 | + |
| 82 | + ResponseBody body = |
| 83 | + ResponseBody.create( |
| 84 | + sseData.getBytes(StandardCharsets.UTF_8), MediaType.parse("text/event-stream")); |
| 85 | + FakeApiResponse response = new FakeApiResponse(Headers.of(), body); |
| 86 | + |
| 87 | + DummyConverter converter = new DummyConverter(); |
| 88 | + ResponseStream<GenerateContentResponse> responseStream = |
| 89 | + new ResponseStream<>(GenerateContentResponse.class, response, converter, "convert"); |
| 90 | + |
| 91 | + Iterator<GenerateContentResponse> iterator = responseStream.iterator(); |
| 92 | + |
| 93 | + assertTrue(iterator.hasNext()); |
| 94 | + GenerateContentResponse response1 = iterator.next(); |
| 95 | + |
| 96 | + assertEquals("valid data", response1.text()); |
| 97 | + |
| 98 | + assertTrue(!iterator.hasNext()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testStreamErrorHandling() throws Exception { |
| 103 | + String sseData = |
| 104 | + "data: {\"candidates\": [{\"content\": {\"parts\": [{\"text\": \"valid data\"}]}}]}\n" |
| 105 | + + "\n" |
| 106 | + + "data: {\"error\": {\"code\": 429, \"message\": \"Quota exceeded\", \"status\": \"RESOURCE_EXHAUSTED\"}}\n" |
| 107 | + + "\n"; |
| 108 | + |
| 109 | + ResponseBody body = |
| 110 | + ResponseBody.create( |
| 111 | + sseData.getBytes(StandardCharsets.UTF_8), MediaType.parse("text/event-stream")); |
| 112 | + FakeApiResponse response = new FakeApiResponse(Headers.of(), body); |
| 113 | + |
| 114 | + DummyConverter converter = new DummyConverter(); |
| 115 | + ResponseStream<GenerateContentResponse> responseStream = |
| 116 | + new ResponseStream<>(GenerateContentResponse.class, response, converter, "convert"); |
| 117 | + |
| 118 | + Iterator<GenerateContentResponse> iterator = responseStream.iterator(); |
| 119 | + |
| 120 | + assertTrue(iterator.hasNext()); |
| 121 | + GenerateContentResponse response1 = iterator.next(); |
| 122 | + assertEquals("valid data", response1.text()); |
| 123 | + |
| 124 | + assertTrue(iterator.hasNext()); |
| 125 | + |
| 126 | + try { |
| 127 | + iterator.next(); |
| 128 | + org.junit.jupiter.api.Assertions.fail("Expected ApiException was not thrown"); |
| 129 | + } catch (com.google.genai.errors.ApiException e) { |
| 130 | + assertEquals(429, e.code()); |
| 131 | + assertEquals("RESOURCE_EXHAUSTED", e.status()); |
| 132 | + assertTrue(e.getMessage().contains("Quota exceeded")); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testMultipleValidEvents() throws Exception { |
| 138 | + String sseData = |
| 139 | + "data: {\"candidates\": [{\"content\": {\"parts\": [{\"text\": \"chunk1\"}]}}]}\n" |
| 140 | + + "\n" |
| 141 | + + "data: {\"candidates\": [{\"content\": {\"parts\": [{\"text\": \"chunk2\"}]}}]}\n" |
| 142 | + + "\n"; |
| 143 | + |
| 144 | + ResponseBody body = |
| 145 | + ResponseBody.create( |
| 146 | + sseData.getBytes(StandardCharsets.UTF_8), MediaType.parse("text/event-stream")); |
| 147 | + FakeApiResponse response = new FakeApiResponse(Headers.of(), body); |
| 148 | + |
| 149 | + DummyConverter converter = new DummyConverter(); |
| 150 | + ResponseStream<GenerateContentResponse> responseStream = |
| 151 | + new ResponseStream<>(GenerateContentResponse.class, response, converter, "convert"); |
| 152 | + |
| 153 | + Iterator<GenerateContentResponse> iterator = responseStream.iterator(); |
| 154 | + |
| 155 | + assertTrue(iterator.hasNext()); |
| 156 | + GenerateContentResponse response1 = iterator.next(); |
| 157 | + assertEquals("chunk1", response1.text()); |
| 158 | + |
| 159 | + assertTrue(iterator.hasNext()); |
| 160 | + GenerateContentResponse response2 = iterator.next(); |
| 161 | + assertEquals("chunk2", response2.text()); |
| 162 | + |
| 163 | + assertTrue(!iterator.hasNext()); |
| 164 | + } |
| 165 | +} |
0 commit comments