use of fi.iki.elonen.NanoHTTPD.Response in project zaproxy by zaproxy.
the class JsonBasedAuthenticationMethodTypeUnitTest method shouldNotUrlEncodeUsernameInPollRequestBody.
@Test
void shouldNotUrlEncodeUsernameInPollRequestBody() throws NullPointerException, IOException {
// Given
String test = "/shouldEncodeSpacesInBody/test";
String pollUrl = "/shouldEncodeSpacesInBody/pollUrl";
String pollData = "{ \"user\": \"" + PostBasedAuthenticationMethod.MSG_USER_PATTERN + "\" }";
String username = "user name";
final List<String> orderedReqData = new ArrayList<>();
this.nano.addHandler(new NanoServerHandler(pollUrl) {
@Override
protected Response serve(IHTTPSession session) {
HashMap<String, String> map = new HashMap<>();
try {
session.parseBody(map);
orderedReqData.add(map.get("postData"));
} catch (Exception e) {
}
return newFixedLengthResponse(LOGGED_IN_BODY);
}
});
HttpMessage testMsg = this.getHttpMessage(test);
HttpMessage pollMsg = this.getHttpMessage(pollUrl);
method.setPollUrl(pollMsg.getRequestHeader().getURI().toString());
method.setPollData(pollData);
User user = mock(User.class);
given(user.getAuthenticationState()).willReturn(new AuthenticationState());
given(user.getAuthenticationCredentials()).willReturn(new UsernamePasswordAuthenticationCredentials(username, ""));
// When/Then
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(orderedReqData.size(), is(1));
assertThat(orderedReqData.get(0), is(pollData.replace(PostBasedAuthenticationMethod.MSG_USER_PATTERN, username)));
}
use of fi.iki.elonen.NanoHTTPD.Response in project zaproxy by zaproxy.
the class FormBasedAuthenticationMethodTypeUnitTest method shouldUrlEncodeUsernameInPollRequestBody.
@Test
void shouldUrlEncodeUsernameInPollRequestBody() throws NullPointerException, IOException {
// Given
String test = "/shouldEncodeSpacesInBody/test";
String pollUrl = "/shouldEncodeSpacesInBody/pollUrl";
String pollData = "user=" + PostBasedAuthenticationMethod.MSG_USER_PATTERN;
String username = "user name";
final List<String> orderedReqData = new ArrayList<>();
this.nano.addHandler(new NanoServerHandler(pollUrl) {
@Override
protected Response serve(IHTTPSession session) {
HashMap<String, String> map = new HashMap<>();
try {
session.parseBody(map);
orderedReqData.add(map.get("postData"));
} catch (Exception e) {
}
return newFixedLengthResponse(LOGGED_IN_BODY);
}
});
HttpMessage testMsg = this.getHttpMessage(test);
HttpMessage pollMsg = this.getHttpMessage(pollUrl);
method.setPollUrl(pollMsg.getRequestHeader().getURI().toString());
method.setPollData(pollData);
User user = mock(User.class);
given(user.getAuthenticationState()).willReturn(new AuthenticationState());
given(user.getAuthenticationCredentials()).willReturn(new UsernamePasswordAuthenticationCredentials(username, ""));
// When/Then
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(orderedReqData.size(), is(1));
assertThat(orderedReqData.get(0), is(pollData.replace(PostBasedAuthenticationMethod.MSG_USER_PATTERN, URLEncoder.encode(username, StandardCharsets.UTF_8.name()))));
}
use of fi.iki.elonen.NanoHTTPD.Response in project zaproxy by zaproxy.
the class AuthenticationMethodPollUrlUnitTest method shouldPollOnFirstRequest.
@Test
void shouldPollOnFirstRequest() throws NullPointerException, IOException {
// Given
String test = "/shouldPollOnFirstRequest/test";
String pollUrl = "/shouldPollOnFirstRequest/pollUrl";
final List<String> orderedReqs = new ArrayList<>();
this.nano.addHandler(new NanoServerHandler(pollUrl) {
@Override
protected Response serve(IHTTPSession session) {
orderedReqs.add(session.getUri());
return newFixedLengthResponse(LOGGED_IN_BODY);
}
});
HttpMessage testMsg = this.getHttpMessage(test);
HttpMessage pollMsg = this.getHttpMessage(pollUrl);
method.setAuthCheckingStrategy(AuthCheckingStrategy.POLL_URL);
method.setPollUrl(pollMsg.getRequestHeader().getURI().toString());
method.setPollFrequencyUnits(AuthPollFrequencyUnits.REQUESTS);
method.setPollFrequency(5);
method.setLoggedInIndicatorPattern(LOGGED_IN_INDICATOR);
User user = mock(User.class);
given(user.getAuthenticationState()).willReturn(new AuthenticationState());
// When/Then
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(orderedReqs.size(), is(1));
assertThat(orderedReqs.get(0), is(pollUrl));
}
use of fi.iki.elonen.NanoHTTPD.Response in project zaproxy by zaproxy.
the class AuthenticationMethodPollUrlUnitTest method shouldPollWhenForced.
@Test
void shouldPollWhenForced() throws NullPointerException, IOException {
// Given
String test = "/shouldPollWhenForced/test";
String pollUrl = "/shouldPollWhenForced/pollUrl";
final List<String> orderedReqs = new ArrayList<>();
User user = mock(User.class);
given(user.getAuthenticationState()).willReturn(new AuthenticationState());
this.nano.addHandler(new NanoServerHandler(pollUrl) {
@Override
protected Response serve(IHTTPSession session) {
orderedReqs.add(session.getUri());
return newFixedLengthResponse(LOGGED_IN_BODY);
}
});
HttpMessage testMsg = this.getHttpMessage(test);
HttpMessage pollMsg = this.getHttpMessage(pollUrl);
method.setAuthCheckingStrategy(AuthCheckingStrategy.POLL_URL);
method.setPollUrl(pollMsg.getRequestHeader().getURI().toString());
method.setPollFrequencyUnits(AuthPollFrequencyUnits.REQUESTS);
method.setPollFrequency(500);
method.setLoggedInIndicatorPattern(LOGGED_IN_INDICATOR);
// When/Then
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(orderedReqs.size(), is(1));
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(orderedReqs.size(), is(1));
user.getAuthenticationState().setLastPollResult(false);
assertThat(method.isAuthenticated(testMsg, user), is(true));
assertThat(orderedReqs.size(), is(2));
assertThat(orderedReqs.get(0), is(pollUrl));
assertThat(orderedReqs.get(1), is(pollUrl));
}
use of fi.iki.elonen.NanoHTTPD.Response in project zaproxy by zaproxy.
the class AuthenticationMethodPollUrlUnitTest method shouldPollEveryFailingRequest.
@Test
void shouldPollEveryFailingRequest() throws NullPointerException, IOException {
// Given
String test = "/shouldPollEveryFailingRequest/test";
String pollUrl = "/shouldPollEveryFailingRequest/pollUrl";
final List<String> orderedReqs = new ArrayList<>();
this.nano.addHandler(new NanoServerHandler(pollUrl) {
@Override
protected Response serve(IHTTPSession session) {
orderedReqs.add(session.getUri());
return newFixedLengthResponse("");
}
});
HttpMessage testMsg = this.getHttpMessage(test);
HttpMessage pollMsg = this.getHttpMessage(pollUrl);
method.setAuthCheckingStrategy(AuthCheckingStrategy.POLL_URL);
method.setPollUrl(pollMsg.getRequestHeader().getURI().toString());
method.setPollFrequencyUnits(AuthPollFrequencyUnits.REQUESTS);
method.setPollFrequency(5);
method.setLoggedInIndicatorPattern(LOGGED_IN_INDICATOR);
User user = mock(User.class);
given(user.getAuthenticationState()).willReturn(new AuthenticationState());
// When/Then
assertThat(method.isAuthenticated(testMsg, user), is(false));
assertThat(orderedReqs.size(), is(1));
assertThat(method.isAuthenticated(testMsg, user), is(false));
assertThat(orderedReqs.size(), is(2));
assertThat(method.isAuthenticated(testMsg, user), is(false));
assertThat(orderedReqs.size(), is(3));
assertThat(method.isAuthenticated(testMsg, user), is(false));
assertThat(orderedReqs.size(), is(4));
}
Aggregations