-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathErrorResponseTest.java
More file actions
91 lines (76 loc) · 2.83 KB
/
Copy pathErrorResponseTest.java
File metadata and controls
91 lines (76 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package serpapi;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
/**
* Test that an error reported in the body of an HTTP 200 response is raised as
* a SerpApiException rather than handed back to the caller as a result.
*/
public class ErrorResponseTest {
/**
* Stubbed HTTP client returning a canned body, so these tests never reach the network.
*/
private static class StubHttp extends SerpApiHttp {
private final String body;
StubHttp(String body) {
super("/search");
this.body = body;
}
@Override
public String get(Map<String, String> parameter) {
return body;
}
}
// Recorded from the google_events engine: HTTP 200, status "Success", no results.
private static final String EMPTY_EVENTS = "{"
+ "\"search_metadata\":{\"status\":\"Success\"},"
+ "\"search_information\":{\"events_results_state\":\"Fully empty\"},"
+ "\"error\":\"Google hasn't returned any results for this query.\"}";
private static final String ORGANIC_RESULTS =
"{\"search_metadata\":{\"status\":\"Success\"},\"organic_results\":[{\"position\":1}]}";
private static SerpApi clientReturning(String body) {
SerpApi serpapi = new SerpApi(new HashMap<>());
serpapi.client = new StubHttp(body);
return serpapi;
}
@Test
public void searchRaisesOnErrorInBody() {
try {
clientReturning(EMPTY_EVENTS).search(new HashMap<>());
fail("expected SerpApiException for a 200 response carrying an error field");
} catch (SerpApiException e) {
assertEquals("Google hasn't returned any results for this query.", e.getMessage());
}
}
@Test
public void searchReturnsResultsWhenBodyHasNoError() throws SerpApiException {
JsonObject results = clientReturning(ORGANIC_RESULTS).search(new HashMap<>());
assertEquals(1, results.getAsJsonArray("organic_results").size());
}
@Test
public void accountRaisesOnErrorInBody() {
try {
clientReturning("{\"error\":\"Invalid API key.\"}").account();
fail("expected SerpApiException for a 200 response carrying an error field");
} catch (SerpApiException e) {
assertEquals("Invalid API key.", e.getMessage());
}
}
@Test
public void locationRaisesOnErrorInBody() {
try {
clientReturning("{\"error\":\"Invalid API key.\"}").location(new HashMap<>());
fail("expected SerpApiException instead of a cast failure on the error object");
} catch (SerpApiException e) {
assertEquals("Invalid API key.", e.getMessage());
}
}
@Test
public void locationReturnsArrayWhenBodyHasNoError() throws SerpApiException {
JsonArray locations = clientReturning("[{\"id\":\"austin\"}]").location(new HashMap<>());
assertEquals(1, locations.size());
}
}