Skip to content

[Test] Fix header value assertion for 401 error #83037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.elasticsearch.xpack.security.user;

import org.apache.http.Header;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.get.GetAction;
import org.elasticsearch.action.get.GetRequest;
Expand All @@ -26,13 +27,15 @@
import org.junit.BeforeClass;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

public class AnonymousUserIntegTests extends SecurityIntegTestCase {
Expand Down Expand Up @@ -75,8 +78,11 @@ public void testAnonymousViaHttp() throws Exception {
assertThat(EntityUtils.toString(response.getEntity()), containsString("security_exception"));
} else {
assertThat(statusCode, is(401));
assertThat(response.getHeader("WWW-Authenticate"), notNullValue());
assertThat(response.getHeader("WWW-Authenticate"), containsString("Basic"));
final List<String> wwwAuthenticateHeaders = Arrays.stream(response.getHeaders())
.filter(header -> "WWW-Authenticate".equalsIgnoreCase(header.getName()))
.map(Header::getValue)
.toList();
assertThat(wwwAuthenticateHeaders, hasItems(containsString("Basic"), containsString("ApiKey")));
assertThat(EntityUtils.toString(response.getEntity()), containsString("security_exception"));
}
}
Expand Down