|
13 | 13 | */
|
14 | 14 | package com.ibm.watson.developer_cloud.service;
|
15 | 15 |
|
16 |
| -import static org.mockserver.integration.ClientAndServer.startClientAndServer; |
17 |
| -import static org.mockserver.model.HttpRequest.request; |
18 |
| -import static org.mockserver.model.HttpResponse.response; |
| 16 | +import static org.mockserver.integration.ClientAndServer.*; |
| 17 | +import static org.mockserver.model.HttpRequest.*; |
| 18 | +import static org.mockserver.model.HttpResponse.*; |
19 | 19 |
|
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
20 | 22 | import java.util.logging.Level;
|
21 | 23 | import java.util.logging.Logger;
|
22 | 24 |
|
23 | 25 | import org.junit.After;
|
24 | 26 | import org.junit.Before;
|
25 | 27 | import org.junit.Test;
|
26 | 28 | import org.mockserver.integration.ClientAndServer;
|
| 29 | +import org.mockserver.model.Header; |
| 30 | +import org.mockserver.model.HttpRequest; |
27 | 31 |
|
28 | 32 | import com.ibm.watson.developer_cloud.WatsonServiceTest;
|
| 33 | +import com.ibm.watson.developer_cloud.http.HttpHeaders; |
29 | 34 | import com.ibm.watson.developer_cloud.personality_insights.v2.PersonalityInsights;
|
30 | 35 | import com.ibm.watson.developer_cloud.personality_insights.v2.PersonalityInsightsTest;
|
31 | 36 |
|
@@ -64,6 +69,14 @@ private void mockAPICallWithError(int code, String errorMessage) {
|
64 | 69 | "{\"code\":" + code + ", \"error\":\"" + errorMessage + "\"}"));
|
65 | 70 | }
|
66 | 71 |
|
| 72 | + /** |
| 73 | + * Mock a successful PersonalityInsights call. |
| 74 | + */ |
| 75 | + private void mockAPICall() { |
| 76 | + mockServer.when(request().withMethod(POST).withPath(GET_PROFILE_PATH)).respond( |
| 77 | + response().withStatusCode(200).withBody("{}")); |
| 78 | + } |
| 79 | + |
67 | 80 | /**
|
68 | 81 | * Service unavailable exception.
|
69 | 82 | */
|
@@ -188,4 +201,27 @@ public void testUnsupportedException() {
|
188 | 201 | service.getProfile(sampleText);
|
189 | 202 | }
|
190 | 203 |
|
| 204 | + @Test |
| 205 | + public void testUserAgentIsSet() { |
| 206 | + mockAPICall(); |
| 207 | + service.getProfile(sampleText); |
| 208 | + mockServer.verify(new HttpRequest().withMethod("POST") |
| 209 | + .withHeader(new Header(HttpHeaders.USER_AGENT, "watson-developer-cloud-java-sdk-2.1.0"))); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void testDefaultHeadersAreSet() { |
| 214 | + final Map<String, String> headers = new HashMap<String, String>(); |
| 215 | + headers.put("name1", "value1"); |
| 216 | + headers.put("name2", "value2"); |
| 217 | + |
| 218 | + final Header expectedHeader1 = new Header("name1", "value1"); |
| 219 | + final Header expectedHeader2 = new Header("name2", "value2"); |
| 220 | + |
| 221 | + service.setDefaultHeaders(headers); |
| 222 | + mockAPICall(); |
| 223 | + service.getProfile(sampleText); |
| 224 | + mockServer.verify(new HttpRequest().withMethod("POST") |
| 225 | + .withHeader(expectedHeader1).withHeader(expectedHeader2)); |
| 226 | + } |
191 | 227 | }
|
0 commit comments