Last active
September 21, 2020 22:48
-
-
Save mpern/3920c0f092a37ad401536be872269fd6 to your computer and use it in GitHub Desktop.
Simulate HTTP Auth for SOAP UI Mock Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.eviware.soapui.support.types.StringToStringsMap | |
def authSucceeded = false | |
// Add those custom properties to your mock server | |
def user = mockRunner.mockService.getPropertyValue("httpUsername") | |
def pass = mockRunner.mockService.getPropertyValue("httpPassword") | |
log.info "checking for credentials: $user:$pass" | |
// get the request headers | |
StringToStringsMap headers = mockRequest.getRequestHeaders() | |
headers.each { | |
if (it.key.equals("Authorization")) { | |
String content = it.value | |
String[] contentArray = content.split() | |
if (contentArray.length == 2) { | |
// decode the auth string | |
String base64Enc = contentArray[1].minus("]") | |
byte[] decoded = base64Enc.decodeBase64() | |
String s = new String(decoded) | |
log.info "Given credentials are: " + s | |
def credentials = s.split(":") | |
authSucceeded = credentials[0].equals(user) && credentials[1].equals(pass) | |
} | |
} | |
} | |
if (!authSucceeded) { | |
def result = new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult( mockRequest ) | |
// yay, correct response status and headers! now even java.net understands that it needs to send authentication | |
mockRequest.httpResponse.status = 401 | |
mockRequest.httpResponse.setHeader("WWW-Authenticate", "Basic realm=\"mysoapuirealm\""); | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment