-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Fix the content type retrieval #7013
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
84c8652
Fix the content type retrieval
benmoriceau 017e9b1
Re-add the original MIME type
benmoriceau 9b9f313
Merge branch 'master' of github.com:airbytehq/airbyte into fix-is-gzip
benmoriceau 66935ce
Fix CS
benmoriceau 6dde12b
Merge branch 'master' of github.com:airbytehq/airbyte into fix-is-gzip
benmoriceau 057e875
Update logic and add a test
benmoriceau c6ef188
Auto format
benmoriceau f08fbd6
Merge branch 'master' of github.com:airbytehq/airbyte into fix-is-gzip
benmoriceau 8049093
PR comments
benmoriceau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
125 changes: 125 additions & 0 deletions
125
airbyte-server/src/test/java/io/airbyte/server/RequestLoggerTest.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,125 @@ | ||
/* | ||
* Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.server; | ||
|
||
import io.airbyte.commons.io.IOs; | ||
import io.airbyte.config.helpers.LogClientSingleton; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerResponseContext; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.slf4j.MDC; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class RequestLoggerTest { | ||
|
||
private static final String VALID_JSON_OBJECT = "{\"valid\":1}"; | ||
private static final String INVALID_JSON_OBJECT = "invalid"; | ||
private static final String ACCEPTED_CONTENT_TYPE = "application/json"; | ||
private static final String NON_ACCEPTED_CONTENT_TYPE = "application/gzip"; | ||
|
||
private static final String METHOD = "POST"; | ||
private static final String REMOTE_ADDR = "123.456.789.101"; | ||
private static final String URL = "/api/v1/test"; | ||
|
||
@Mock | ||
private HttpServletRequest mServletRequest; | ||
|
||
@Mock | ||
private ContainerRequestContext mRequestContext; | ||
|
||
@Mock | ||
private ContainerResponseContext mResponseContext; | ||
|
||
private RequestLogger requestLogger; | ||
|
||
@BeforeEach | ||
public void init() throws Exception { | ||
Mockito.when(mRequestContext.getMethod()) | ||
.thenReturn(METHOD); | ||
|
||
Mockito.when(mServletRequest.getMethod()) | ||
.thenReturn(METHOD); | ||
Mockito.when(mServletRequest.getRemoteAddr()) | ||
.thenReturn(REMOTE_ADDR); | ||
Mockito.when(mServletRequest.getRequestURI()) | ||
.thenReturn(URL); | ||
} | ||
|
||
private static final int ERROR_CODE = 401; | ||
private static final int SUCCESS_CODE = 200; | ||
|
||
private static final String errorPrefix = RequestLogger | ||
.createLogPrefix(REMOTE_ADDR, METHOD, ERROR_CODE, URL) | ||
.toString(); | ||
|
||
private static final String successPrefix = RequestLogger | ||
.createLogPrefix(REMOTE_ADDR, METHOD, SUCCESS_CODE, URL) | ||
.toString(); | ||
|
||
static Stream<Arguments> logScenarios() { | ||
return Stream.of( | ||
Arguments.of(INVALID_JSON_OBJECT, NON_ACCEPTED_CONTENT_TYPE, ERROR_CODE, errorPrefix), | ||
Arguments.of(INVALID_JSON_OBJECT, ACCEPTED_CONTENT_TYPE, ERROR_CODE, errorPrefix), | ||
Arguments.of(VALID_JSON_OBJECT, NON_ACCEPTED_CONTENT_TYPE, ERROR_CODE, errorPrefix), | ||
Arguments.of(VALID_JSON_OBJECT, ACCEPTED_CONTENT_TYPE, ERROR_CODE, errorPrefix + " - " + VALID_JSON_OBJECT), | ||
Arguments.of(INVALID_JSON_OBJECT, NON_ACCEPTED_CONTENT_TYPE, SUCCESS_CODE, successPrefix), | ||
Arguments.of(INVALID_JSON_OBJECT, ACCEPTED_CONTENT_TYPE, SUCCESS_CODE, successPrefix), | ||
Arguments.of(VALID_JSON_OBJECT, NON_ACCEPTED_CONTENT_TYPE, SUCCESS_CODE, successPrefix), | ||
Arguments.of(VALID_JSON_OBJECT, ACCEPTED_CONTENT_TYPE, SUCCESS_CODE, successPrefix + " - " + VALID_JSON_OBJECT)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("logScenarios") | ||
@DisplayName("Check that the proper log is produced based on the scenario") | ||
public void test(String inputByteBuffer, String contentType, int status, String expectedLog) throws IOException { | ||
// set up the mdc so that actually log to a file, so that we can verify that file logging captures | ||
// threads. | ||
final Path jobRoot = Files.createTempDirectory(Path.of("/tmp"), "mdc_test"); | ||
LogClientSingleton.setJobMdc(jobRoot); | ||
|
||
// We have to instanciate the logger here, because the MDC config has been changed to log in a | ||
// temporary file. | ||
requestLogger = new RequestLogger(MDC.getCopyOfContextMap(), mServletRequest); | ||
|
||
Mockito.when(mRequestContext.getEntityStream()) | ||
.thenReturn(new ByteArrayInputStream(inputByteBuffer.getBytes())); | ||
|
||
Mockito.when(mResponseContext.getStatus()) | ||
.thenReturn(status); | ||
|
||
Mockito.when(mServletRequest.getHeader("Content-Type")) | ||
.thenReturn(contentType); | ||
|
||
// This is call to set the requestBody variable in the RequestLogger | ||
requestLogger.filter(mRequestContext); | ||
requestLogger.filter(mRequestContext, mResponseContext); | ||
|
||
String expectedLogLevel = status == SUCCESS_CODE ? "INFO" : "ERROR"; | ||
|
||
final Path logPath = jobRoot.resolve(LogClientSingleton.LOG_FILENAME); | ||
final String logs = IOs.readFile(logPath); | ||
final Stream<String> matchingLines = logs.lines() | ||
.filter(line -> line.endsWith(expectedLog)) | ||
.filter(line -> line.contains(expectedLogLevel)); | ||
|
||
Assertions.assertThat(matchingLines).hasSize(1); | ||
} | ||
|
||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're using Jackson for JSON manipulation almost everywhere and we have a few helpers for it. Can we use
Jsons.tryDeserialize
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done