Skip to content

Instantly share code, notes, and snippets.

@js1972
Created May 1, 2014 08:41
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save js1972/11447197 to your computer and use it in GitHub Desktop.
Save js1972/11447197 to your computer and use it in GitHub Desktop.
Basic Auth check in groovy for SoapUI. When using a SOAP Mock in SoapUI there is no standard way to check the authentication provided by the caller. This script can be added to your mock in the "OnRequest Script" section and allows you to check for the correct credentials. If you test this from SoapUI as well then you need to set the Authenticat…
import com.eviware.soapui.support.types.StringToStringsMap
def authSucceeded = false
// 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(":")
assert credentials[0].equals("jscott")
assert credentials[1].equals("zzz")
log.info "Mock service authenticated for credentials: " + s
authSucceeded = true
}
}
}
assert authSucceeded == true
@niklasskeppstedt
Copy link

Exactly what i was looking for! Thank you! Copy/Paste edit credentials - Done!

@LebogangMokgabudi
Copy link

On what level do you copy/paste credentials???

@mrgsathyaraj
Copy link

Style=Header this only its fetching,is thereany way to get the Style=Template values

@Sladjana21
Copy link

Perfect! Thank you!

@enakamura3
Copy link

Good! Thank you mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment