-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#445: Specify encoding when creating Gitlab decoration requests
When calling the Gitlab API with content that contains accented or cyrillic characters, the API returns an error response and fails to complete the decoration. Explicitly setting the encoding for all body content to UTF-8 on Gitlab API requests results in the content being encoded in a way for the Gitlab API handles correctly.
- Loading branch information
Showing
6 changed files
with
116 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
.../java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabRestClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.gitlab; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.LinkHeaderReader; | ||
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.gitlab.model.MergeRequestNote; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpEntityEnclosingRequest; | ||
import org.apache.http.StatusLine; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class GitlabRestClientTest { | ||
|
||
private final CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); | ||
private final LinkHeaderReader linkHeaderReader = mock(LinkHeaderReader.class); | ||
private final ObjectMapper objectMapper = mock(ObjectMapper.class); | ||
|
||
@Test | ||
void checkErrorThrownOnNonSuccessResponseStatus() throws IOException { | ||
GitlabRestClient underTest = new GitlabRestClient("http://url.test/api", "token", linkHeaderReader, objectMapper, () -> closeableHttpClient); | ||
|
||
CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); | ||
StatusLine statusLine = mock(StatusLine.class); | ||
when(statusLine.getStatusCode()).thenReturn(500); | ||
when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine); | ||
when(closeableHttpClient.execute(any())).thenReturn(closeableHttpResponse); | ||
|
||
MergeRequestNote mergeRequestNote = mock(MergeRequestNote.class); | ||
when(mergeRequestNote.getContent()).thenReturn("note"); | ||
|
||
assertThatThrownBy(() -> underTest.addMergeRequestDiscussion(101, 99, mergeRequestNote)) | ||
.isExactlyInstanceOf(IllegalStateException.class) | ||
.hasMessage("An unexpected response code was returned from the Gitlab API - Expected: 201, Got: 500") | ||
.hasNoCause(); | ||
|
||
ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.forClass(HttpUriRequest.class); | ||
verify(closeableHttpClient).execute(requestArgumentCaptor.capture()); | ||
|
||
HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestArgumentCaptor.getValue(); | ||
|
||
assertThat(request.getRequestLine().getMethod()).isEqualTo("POST"); | ||
assertThat(request.getRequestLine().getUri()).isEqualTo("http://url.test/api/projects/101/merge_requests/99/discussions"); | ||
assertThat(request.getEntity().getContent()).hasContent("body=note"); | ||
} | ||
|
||
@Test | ||
void checkCorrectEncodingUsedOnMergeRequestDiscussion() throws IOException { | ||
CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); | ||
StatusLine statusLine = mock(StatusLine.class); | ||
when(statusLine.getStatusCode()).thenReturn(201); | ||
when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine); | ||
HttpEntity httpEntity = mock(HttpEntity.class); | ||
when(closeableHttpResponse.getEntity()).thenReturn(httpEntity); | ||
when(closeableHttpClient.execute(any())).thenReturn(closeableHttpResponse); | ||
|
||
MergeRequestNote mergeRequestNote = new MergeRequestNote("Merge request note"); | ||
|
||
GitlabRestClient underTest = new GitlabRestClient("http://api.url", "token", linkHeaderReader, objectMapper, () -> closeableHttpClient); | ||
underTest.addMergeRequestDiscussion(123, 321, mergeRequestNote); | ||
|
||
ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.forClass(HttpUriRequest.class); | ||
verify(closeableHttpClient).execute(requestArgumentCaptor.capture()); | ||
|
||
HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestArgumentCaptor.getValue(); | ||
|
||
assertThat(request.getRequestLine().getMethod()).isEqualTo("POST"); | ||
assertThat(request.getRequestLine().getUri()).isEqualTo("http://api.url/projects/123/merge_requests/321/discussions"); | ||
assertThat(request.getEntity().getContent()).hasContent("body=Merge+request+note"); | ||
} | ||
|
||
} |