@Test
public void testBasicAuth() throws Exception {
Service service = Service.create(serviceName);
service.addPort(fakePortName, "http://schemas.xmlsoap.org/soap/",
"http://localhost:" + PORT + "/SoapContext/SoapPort");
Greeter greeter = service.getPort(fakePortName, Greeter.class);
try {
//try the jaxws way
BindingProvider bp = (BindingProvider)greeter;
bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "BJ");
bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
String s = greeter.greetMe("secure");
assertEquals("Hello BJ", s);
bp.getRequestContext().remove(BindingProvider.USERNAME_PROPERTY);
bp.getRequestContext().remove(BindingProvider.PASSWORD_PROPERTY);
//try setting on the conduit directly
Client client = ClientProxy.getClient(greeter);
HTTPConduit httpConduit = (HTTPConduit)client.getConduit();
AuthorizationPolicy policy = new AuthorizationPolicy();
policy.setUserName("BJ2");
policy.setPassword("pswd");
httpConduit.setAuthorization(policy);
s = greeter.greetMe("secure");
assertEquals("Hello BJ2", s);
} catch (UndeclaredThrowableException ex) {
throw (Exception)ex.getCause();
}
}