Skip to content

Commit

Permalink
fix(artifacts/bitbuket): added ACCEPT Header when using token auth (#…
Browse files Browse the repository at this point in the history
…5813) (#5823)

* fix(artifacts/bitbuket): added ACCEPT Header when using token auth

* fix(artifacts/bitbuket): changed for constant MediaType.APPLICATION_JSON_VALUE

* fix(artifacts/bitbuket): added 'Accept: application/json' header on unit tests

(cherry picked from commit 668d77e)

Co-authored-by: David Perez <43676929+Avi1235@users.noreply.github.com>
  • Loading branch information
mergify[bot] and Avi1235 authored Nov 25, 2022
1 parent 6185850 commit cbe7bca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.netflix.spinnaker.clouddriver.artifacts.bitbucket;

import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;

import com.google.common.collect.ImmutableList;
Expand All @@ -28,6 +29,7 @@
import java.util.Optional;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

@NonnullByDefault
@Slf4j
Expand All @@ -48,6 +50,7 @@ protected Headers getHeaders(BitbucketArtifactAccount account) {
Optional<String> token = account.getTokenAsString();
if (token.isPresent()) {
headers.set(AUTHORIZATION, "Bearer " + token.get());
headers.set(ACCEPT, MediaType.APPLICATION_JSON_VALUE);
log.info("Loaded credentials for Bitbucket Artifact Account {}", account.getName());
return headers.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;

import com.github.tomakehurst.wiremock.WireMockServer;
Expand All @@ -33,6 +34,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
import org.springframework.http.MediaType;
import ru.lanwen.wiremock.ext.WiremockResolver;

@ExtendWith({WiremockResolver.class, TempDirectory.class})
Expand All @@ -47,7 +49,12 @@ void downloadWithToken(@WiremockResolver.Wiremock WireMockServer server) throws
BitbucketArtifactAccount account =
BitbucketArtifactAccount.builder().name("my-bitbucket-account").token("abc").build();

runTestCase(server, account, m -> m.withHeader(AUTHORIZATION, equalTo("Bearer abc")));
runTestCase(
server,
account,
m ->
m.withHeader(AUTHORIZATION, equalTo("Bearer abc"))
.withHeader(ACCEPT, equalTo(MediaType.APPLICATION_JSON_VALUE)));
}

@Test
Expand All @@ -63,7 +70,12 @@ void downloadWithTokenFromFile(
.tokenFile(authFile.toAbsolutePath().toString())
.build();

runTestCase(server, account, m -> m.withHeader(AUTHORIZATION, equalTo("Bearer zzz")));
runTestCase(
server,
account,
m ->
m.withHeader(AUTHORIZATION, equalTo("Bearer zzz"))
.withHeader(ACCEPT, equalTo(MediaType.APPLICATION_JSON_VALUE)));
}

@Test
Expand All @@ -79,11 +91,21 @@ void downloadWithTokenFromFileWithReloadHeaders(
.tokenFile(authFile.toAbsolutePath().toString())
.build();

runTestCase(server, account, m -> m.withHeader(AUTHORIZATION, equalTo("Bearer zzz")));
runTestCase(
server,
account,
m ->
m.withHeader(AUTHORIZATION, equalTo("Bearer zzz"))
.withHeader(ACCEPT, equalTo(MediaType.APPLICATION_JSON_VALUE)));

Files.write(authFile, "aaa".getBytes());

runTestCase(server, account, m -> m.withHeader(AUTHORIZATION, equalTo("Bearer aaa")));
runTestCase(
server,
account,
m ->
m.withHeader(AUTHORIZATION, equalTo("Bearer aaa"))
.withHeader(ACCEPT, equalTo(MediaType.APPLICATION_JSON_VALUE)));
}

@Test
Expand Down

0 comments on commit cbe7bca

Please sign in to comment.