Search code examples
javamaventestingrest-assuredhttp-status-code-401

Getting 401 response code in rest assured tests


Im trying to fix my rest assured test, because after i added

    <dependency>
        <groupId>de.ruedigermoeller</groupId>
        <artifactId>fst</artifactId>
        <version>3.0.4-jdk17</version>
</dependency>

And now in every single test i get:

java.lang.AssertionError: Expected :200 Actual :401

Im using:

 <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.1.1</version>
        <scope>test</scope>
    </dependency>

Im passing every header needed including Authorization header with username and password encoded. This header is like this:

new Header("Authorization", "Basic XXXXYYYYXXSDSDDS=");

I will be grateful for any help. Thanks.


Solution

  • It looks like something is overriding your headers. Can you add log().all() to the RestAssured calls so that you can see if you have the Authorization header as part of the request headers? Thats the first step to solve the issue.

    Alternatively you can use this for forcing preemtive basic auth.

    given().auth()
      .preemptive()
      .basic("user1", "user1Pass")