繁体   English   中英

在io.restassured api测试中使用Windows身份验证

[英]using windows authentication in io.restassured api testing

我们正在尝试测试使用Windows身份验证的api。 我正在使用放心的包来测试api。 有任何建议如何将登录用户的窗口分配给api请求标头?

重写了我的答案所以,目前很难做到,因为你需要为REST Assured配置HTTPClient,但它只支持弃用的AbstractHttpClient 这是我的实施。 但我没有能力测试它......

import io.restassured.RestAssured;
import org.apache.http.annotation.Contract;
import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeFactory;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthSchemeRegistry;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.impl.auth.BasicSchemeFactory;
import org.apache.http.impl.auth.DigestSchemeFactory;
import org.apache.http.impl.auth.win.WindowsCredentialsProvider;
import org.apache.http.impl.auth.win.WindowsNegotiateScheme;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.SystemDefaultCredentialsProvider;
import org.apache.http.impl.client.SystemDefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;
import static io.restassured.config.HttpClientConfig.httpClientConfig;
import static java.net.HttpURLConnection.HTTP_OK;

public class WinHttpClientTest {

    @Test
    public void test() {

        @Contract(threading = ThreadingBehavior.IMMUTABLE)
        class WindowsNTLMSchemeFactory implements AuthSchemeProvider, AuthSchemeFactory {

            private final String servicePrincipalName;

            public WindowsNTLMSchemeFactory(final String servicePrincipalName) {
                super();
                this.servicePrincipalName = servicePrincipalName;
            }

            @Override
            public AuthScheme create(final HttpContext context) {
                return new WindowsNegotiateScheme(AuthSchemes.NTLM, servicePrincipalName);
            }

            @Override
            public AuthScheme newInstance(HttpParams params) {
                return new WindowsNegotiateScheme(AuthSchemes.NTLM, null) ;
            }
        }

        @Contract(threading = ThreadingBehavior.IMMUTABLE)
        class WindowsNegotiateSchemeFactory implements AuthSchemeProvider, AuthSchemeFactory {

            private final String servicePrincipalName;

            public WindowsNegotiateSchemeFactory(final String servicePrincipalName) {
                super();
                this.servicePrincipalName = servicePrincipalName;
            }

            @Override
            public AuthScheme create(final HttpContext context) {
                return new WindowsNegotiateScheme(AuthSchemes.SPNEGO, servicePrincipalName);
            }

            @Override
            public AuthScheme newInstance(HttpParams params) {
                return new WindowsNegotiateScheme(AuthSchemes.SPNEGO, null);
            }
        }

        AuthSchemeRegistry authSceme = new AuthSchemeRegistry();
        authSceme.register(AuthSchemes.BASIC, new BasicSchemeFactory());
        authSceme.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
        authSceme.register(AuthSchemes.NTLM, new WindowsNTLMSchemeFactory(null));
        authSceme.register(AuthSchemes.SPNEGO, new WindowsNegotiateSchemeFactory(null));

        final CredentialsProvider credsProvider = new WindowsCredentialsProvider(new SystemDefaultCredentialsProvider());
        AbstractHttpClient httpClient = new SystemDefaultHttpClient();
        httpClient.setAuthSchemes(authSceme);
        httpClient.setCredentialsProvider(credsProvider);

        RestAssured.config = RestAssured.config().httpClient(httpClientConfig().httpClientFactory(() -> httpClient));
        given()
                .log().all()
                .when()
                .get("http://httpbin.org/get")
                .then()
                .log().all()
                .statusCode(HTTP_OK);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM