Skip to content

Commit 16ba2a5

Browse files
committed
Add test cases for API key and HTTP basic auth
1 parent f616605 commit 16ba2a5

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.swagger.client.auth;
2+
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
6+
import static org.junit.Assert.*;
7+
import org.junit.*;
8+
9+
public class ApiKeyAuthTest {
10+
@Test
11+
public void testProcessParamsInQuery() {
12+
Map<String, String> queryParams = new HashMap<String, String>();
13+
Map<String, String> headerParams = new HashMap<String, String>();
14+
15+
ApiKeyAuth auth = new ApiKeyAuth("query", "api_key");
16+
auth.setApiKey("my-api-key");
17+
auth.processParams(queryParams, headerParams);
18+
19+
assertEquals(1, queryParams.size());
20+
assertEquals("my-api-key", queryParams.get("api_key"));
21+
// no changes to header parameters
22+
assertEquals(0, headerParams.size());
23+
}
24+
25+
@Test
26+
public void testProcessParamsInHeaderWithPrefix() {
27+
Map<String, String> queryParams = new HashMap<String, String>();
28+
Map<String, String> headerParams = new HashMap<String, String>();
29+
30+
ApiKeyAuth auth = new ApiKeyAuth("header", "X-API-TOKEN");
31+
auth.setApiKey("my-api-token");
32+
auth.setApiKeyPrefix("Token");
33+
auth.processParams(queryParams, headerParams);
34+
35+
// no changes to query parameters
36+
assertEquals(0, queryParams.size());
37+
assertEquals(1, headerParams.size());
38+
assertEquals("Token my-api-token", headerParams.get("X-API-TOKEN"));
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.swagger.client.auth;
2+
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
6+
import static org.junit.Assert.*;
7+
import org.junit.*;
8+
9+
public class HttpBasicAuthTest {
10+
HttpBasicAuth auth = null;
11+
12+
@Before
13+
public void setup() {
14+
auth = new HttpBasicAuth();
15+
}
16+
17+
@Test
18+
public void testProcessParams() {
19+
Map<String, String> queryParams = new HashMap<String, String>();
20+
Map<String, String> headerParams = new HashMap<String, String>();
21+
22+
auth.setUsername("my-username");
23+
auth.setPassword("my-password");
24+
auth.processParams(queryParams, headerParams);
25+
26+
// no changes to query parameters
27+
assertEquals(0, queryParams.size());
28+
assertEquals(1, headerParams.size());
29+
// the string below is base64-encoded result of "my-username:my-password" with the "Basic " prefix
30+
final String expected = "Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=";
31+
assertEquals(expected, headerParams.get("Authorization"));
32+
}
33+
}

0 commit comments

Comments
 (0)