Add authentication helper methods and test cases

This commit is contained in:
xhh 2015-05-29 11:28:03 +08:00
parent 4d25d264c4
commit 6eb1a89205
3 changed files with 144 additions and 0 deletions

View File

@ -24,4 +24,48 @@ public class Configuration {
public static Authentication getAuthentication(String authName) { public static Authentication getAuthentication(String authName) {
return AUTH.get(authName); return AUTH.get(authName);
} }
/** Set username for the first HTTP basic authentication. */
public static void setUsername(String username) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/** Set password for the first HTTP basic authentication. */
public static void setPassword(String password) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/** Set API key value for the first API key authentication. */
public static void setApiKey(String apiKey) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKey(apiKey);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
/** Set API key prefix for the first API key authentication. */
public static void setApiKeyPrefix(String apiKeyPrefix) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
} }

View File

@ -28,4 +28,48 @@ public class Configuration {
public static Authentication getAuthentication(String authName) { public static Authentication getAuthentication(String authName) {
return AUTH.get(authName); return AUTH.get(authName);
} }
/** Set username for the first HTTP basic authentication. */
public static void setUsername(String username) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/** Set password for the first HTTP basic authentication. */
public static void setPassword(String password) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/** Set API key value for the first API key authentication. */
public static void setApiKey(String apiKey) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKey(apiKey);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
/** Set API key prefix for the first API key authentication. */
public static void setApiKeyPrefix(String apiKeyPrefix) {
for (Authentication auth : AUTH.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
} }

View File

@ -0,0 +1,56 @@
package io.swagger.client;
import io.swagger.client.auth.*;
import static org.junit.Assert.*;
import org.junit.*;
public class ConfigurationTest {
@Test
public void testGetAuthentication() {
Authentication auth = Configuration.getAuthentication("api_key");
assertNotNull(auth);
assertTrue(auth instanceof ApiKeyAuth);
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth;
assertEquals("header", apiKeyAuth.getLocation());
assertEquals("api_key", apiKeyAuth.getParamName());
auth = Configuration.getAuthentication("petstore_auth");
assertTrue(auth instanceof OAuth);
assertNull(Configuration.getAuthentication("unknown"));
}
@Test
public void testSetUsername() {
try {
Configuration.setUsername("my-username");
fail("should throw RuntimeException");
} catch (RuntimeException e) {
}
}
@Test
public void testSetPassword() {
try {
Configuration.setPassword("my-password");
fail("should throw RuntimeException");
} catch (RuntimeException e) {
}
}
@Test
public void testSetApiKeyAndPrefix() {
ApiKeyAuth auth = (ApiKeyAuth) Configuration.getAuthentication("api_key");
auth.setApiKey(null);
auth.setApiKeyPrefix(null);
Configuration.setApiKey("my-api-key");
Configuration.setApiKeyPrefix("Token");
assertEquals("my-api-key", auth.getApiKey());
assertEquals("Token", auth.getApiKeyPrefix());
auth.setApiKey(null);
auth.setApiKeyPrefix(null);
}
}