-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMarkdownApiTest.java
More file actions
67 lines (54 loc) · 1.84 KB
/
Copy pathMarkdownApiTest.java
File metadata and controls
67 lines (54 loc) · 1.84 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
package serpapi;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
/**
* Test SerpApi.markdown() method.
*
* Offline: the HTTP client is stubbed, so no network call and no SERPAPI_KEY
* are needed. What matters here is the request this client builds, which can
* be asserted without a live backend.
*/
public class MarkdownApiTest {
/**
* Stubbed HTTP client that records the query it was asked to send.
*/
private static class RecordingHttp extends SerpApiHttp {
Map<String, String> recorded;
RecordingHttp() {
super("/search");
}
@Override
public String get(Map<String, String> parameter) {
this.recorded = parameter;
return "## Search results\n\n1. [Coffee](https://example.com)\n";
}
}
@Test
public void markdownRequestsMdOutput() throws SerpApiException {
SerpApi serpapi = new SerpApi(new HashMap<>());
RecordingHttp http = new RecordingHttp();
serpapi.client = http;
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "coffee");
String content = serpapi.markdown(parameter);
assertEquals("md", http.recorded.get("output"));
assertEquals("coffee", http.recorded.get("q"));
assertEquals("/search", http.path);
assertTrue(content.startsWith("## Search results"));
}
@Test
public void markdownMergesDefaultParameter() throws SerpApiException {
Map<String, String> auth = new HashMap<>();
auth.put("api_key", "secret");
auth.put("engine", "google");
SerpApi serpapi = new SerpApi(auth);
RecordingHttp http = new RecordingHttp();
serpapi.client = http;
serpapi.markdown(new HashMap<>());
assertEquals("secret", http.recorded.get("api_key"));
assertEquals("google", http.recorded.get("engine"));
assertEquals("md", http.recorded.get("output"));
}
}