|
| 1 | +package serpapi; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.sun.net.httpserver.HttpServer; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.net.InetSocketAddress; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.concurrent.atomic.AtomicReference; |
| 14 | + |
| 15 | +import static org.junit.Assert.*; |
| 16 | + |
| 17 | +/** Offline tests for Image API multipart upload support. */ |
| 18 | +public class ImageApiTest { |
| 19 | + |
| 20 | + private static class RecordingHttp extends SerpApiHttp { |
| 21 | + Map<String, String> recorded; |
| 22 | + byte[] recordedImage; |
| 23 | + String response = "{\"message\":\"Image uploaded successfully.\",\"image_id\":\"image-123\"}"; |
| 24 | + |
| 25 | + RecordingHttp() { |
| 26 | + super("/search"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public String postMultipart(Map<String, String> parameter, byte[] image) { |
| 31 | + recorded = parameter; |
| 32 | + recordedImage = image; |
| 33 | + return response; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static SerpApi client(RecordingHttp http) { |
| 38 | + Map<String, String> defaults = new HashMap<>(); |
| 39 | + defaults.put("api_key", "client-key"); |
| 40 | + defaults.put("engine", "google_lens"); |
| 41 | + SerpApi client = new SerpApi(defaults); |
| 42 | + client.client = http; |
| 43 | + return client; |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void uploadsPathAndReturnsImageId() throws Exception { |
| 48 | + Path image = Files.createTempFile("serpapi-image-", ".png"); |
| 49 | + byte[] imageData = new byte[] {1, 2, 3}; |
| 50 | + Files.write(image, imageData); |
| 51 | + try { |
| 52 | + RecordingHttp http = new RecordingHttp(); |
| 53 | + JsonObject result = client(http).uploadImage(image); |
| 54 | + |
| 55 | + assertEquals("image-123", result.get("image_id").getAsString()); |
| 56 | + assertArrayEquals(imageData, http.recordedImage); |
| 57 | + assertEquals("/image", http.path); |
| 58 | + assertEquals("client-key", http.recorded.get("api_key")); |
| 59 | + assertFalse(http.recorded.containsKey("engine")); |
| 60 | + } finally { |
| 61 | + Files.deleteIfExists(image); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void acceptsRawBytesAndCustomFormFields() throws Exception { |
| 67 | + byte[] image = new byte[] {1, 2, 3}; |
| 68 | + Map<String, String> fields = new HashMap<>(); |
| 69 | + fields.put("api_key", "request-key"); |
| 70 | + fields.put("zero_trace", "true"); |
| 71 | + RecordingHttp http = new RecordingHttp(); |
| 72 | + |
| 73 | + JsonObject result = client(http).uploadImage(image, fields); |
| 74 | + |
| 75 | + assertEquals("image-123", result.get("image_id").getAsString()); |
| 76 | + assertSame(image, http.recordedImage); |
| 77 | + assertEquals("request-key", http.recorded.get("api_key")); |
| 78 | + assertEquals("true", http.recorded.get("zero_trace")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void httpClientSendsMultipartBody() throws Exception { |
| 83 | + AtomicReference<String> contentType = new AtomicReference<>(); |
| 84 | + AtomicReference<String> requestBody = new AtomicReference<>(); |
| 85 | + HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); |
| 86 | + server.createContext("/image", exchange -> { |
| 87 | + contentType.set(exchange.getRequestHeaders().getFirst("Content-Type")); |
| 88 | + requestBody.set(new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8)); |
| 89 | + byte[] response = "{\"image_id\":\"local-test\"}".getBytes(StandardCharsets.UTF_8); |
| 90 | + exchange.sendResponseHeaders(200, response.length); |
| 91 | + exchange.getResponseBody().write(response); |
| 92 | + exchange.close(); |
| 93 | + }); |
| 94 | + server.start(); |
| 95 | + |
| 96 | + String originalBackend = SerpApiHttp.BACKEND; |
| 97 | + byte[] image = "fake-png-data".getBytes(StandardCharsets.UTF_8); |
| 98 | + try { |
| 99 | + SerpApiHttp.BACKEND = "http://localhost:" + server.getAddress().getPort(); |
| 100 | + SerpApiHttp http = new SerpApiHttp("/image"); |
| 101 | + Map<String, String> fields = new HashMap<>(); |
| 102 | + fields.put("api_key", "test-key"); |
| 103 | + |
| 104 | + assertTrue(http.postMultipart(fields, image).contains("local-test")); |
| 105 | + assertTrue(contentType.get().startsWith("multipart/form-data; boundary=")); |
| 106 | + assertTrue(requestBody.get().contains("name=\"api_key\"\r\n\r\ntest-key")); |
| 107 | + assertTrue(requestBody.get().contains("name=\"image\"; filename=\"image\"")); |
| 108 | + assertTrue(requestBody.get().contains("Content-Type: application/octet-stream")); |
| 109 | + assertTrue(requestBody.get().contains("fake-png-data")); |
| 110 | + } finally { |
| 111 | + SerpApiHttp.BACKEND = originalBackend; |
| 112 | + server.stop(0); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void raisesErrorReturnedByImageApi() { |
| 118 | + RecordingHttp http = new RecordingHttp(); |
| 119 | + http.response = "{\"error\":\"Unsupported image format.\"}"; |
| 120 | + |
| 121 | + try { |
| 122 | + client(http).uploadImage(new byte[0]); |
| 123 | + fail("expected SerpApiException"); |
| 124 | + } catch (SerpApiException e) { |
| 125 | + assertEquals("Unsupported image format.", e.getMessage()); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments