-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Improve configuration error handling of HttpAppender #3438
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bfceeb4
HttpAppender fix and HttpAppenderBuilderTest
Suvrat1629 46387d8
Merge branch 'apache:2.x' into 2.x
Suvrat1629 0f0448e
formatting
Suvrat1629 ff71db9
Remove wildcard imports
ppkarwasz 9aa1c49
removed the mocked StatusLogger
Suvrat1629 28a3817
minor change
Suvrat1629 0b28893
Changed some comments
Suvrat1629 6cfad6d
formatting
Suvrat1629 9254351
Add changelog
ppkarwasz 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
138 changes: 138 additions & 0 deletions
138
...re-test/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderBuilderTest.java
This file contains hidden or 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,138 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.logging.log4j.core.appender; | ||
|
||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.core.Layout; | ||
import org.apache.logging.log4j.core.config.Configuration; | ||
import org.apache.logging.log4j.core.config.DefaultConfiguration; | ||
import org.apache.logging.log4j.core.config.Property; | ||
import org.apache.logging.log4j.core.layout.JsonLayout; | ||
import org.apache.logging.log4j.core.net.ssl.SslConfiguration; | ||
import org.apache.logging.log4j.status.StatusLogger; | ||
import org.apache.logging.log4j.test.ListStatusListener; | ||
import org.apache.logging.log4j.test.junit.UsingStatusListener; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
class HttpAppenderBuilderTest { | ||
|
||
// Mock the LOGGER to verify error logs | ||
private final StatusLogger mockStatusLogger = Mockito.mock(StatusLogger.class); | ||
|
||
private HttpAppender.Builder<?> getBuilder() { | ||
Configuration mockConfig = new DefaultConfiguration(); | ||
return HttpAppender.newBuilder().setConfiguration(mockConfig).setName("TestHttpAppender"); // Name is required | ||
} | ||
|
||
@Test | ||
@UsingStatusListener | ||
void testBuilderWithoutUrl(final ListStatusListener listener) throws Exception { | ||
// Build the HttpAppender without URL | ||
HttpAppender appender = HttpAppender.newBuilder() | ||
.setConfiguration(new DefaultConfiguration()) | ||
.setName("TestAppender") | ||
.setLayout(JsonLayout.createDefaultLayout()) // Providing a layout here | ||
.build(); | ||
|
||
// Verify that the error message for missing URL is captured | ||
assertThat(listener.findStatusData(Level.ERROR)) | ||
.anyMatch(statusData -> | ||
statusData.getMessage().getFormattedMessage().contains("HttpAppender requires URL to be set.")); | ||
} | ||
|
||
@Test | ||
@UsingStatusListener | ||
void testBuilderWithUrlAndWithoutLayout(final ListStatusListener listener) throws Exception { | ||
// Build the HttpAppender with URL but without Layout | ||
HttpAppender appender = HttpAppender.newBuilder() | ||
.setConfiguration(new DefaultConfiguration()) | ||
.setName("TestAppender") | ||
.setUrl(new URL("http://localhost:8080/logs")) // Providing the URL | ||
.build(); | ||
|
||
// Verify that the error message for missing layout is captured | ||
assertThat(listener.findStatusData(Level.ERROR)).anyMatch(statusData -> statusData | ||
.getMessage() | ||
.getFormattedMessage() | ||
.contains("HttpAppender requires a layout to be set.")); | ||
} | ||
|
||
@Test | ||
void testBuilderWithValidConfiguration() throws Exception { | ||
URL url = new URL("http://example.com"); | ||
Layout<?> layout = JsonLayout.createDefaultLayout(); // Valid layout | ||
|
||
HttpAppender.Builder<?> builder = getBuilder().setUrl(url).setLayout(layout); | ||
|
||
HttpAppender appender = builder.build(); | ||
assertNotNull(appender, "HttpAppender should be created with valid configuration."); | ||
} | ||
|
||
@Test | ||
void testBuilderWithCustomMethod() throws Exception { | ||
URL url = new URL("http://example.com"); | ||
Layout<?> layout = JsonLayout.createDefaultLayout(); | ||
String customMethod = "PUT"; | ||
|
||
HttpAppender.Builder<?> builder = | ||
getBuilder().setUrl(url).setLayout(layout).setMethod(customMethod); | ||
|
||
HttpAppender appender = builder.build(); | ||
assertNotNull(appender, "HttpAppender should be created with a custom HTTP method."); | ||
} | ||
|
||
@Test | ||
void testBuilderWithHeaders() throws Exception { | ||
URL url = new URL("http://example.com"); | ||
Layout<?> layout = JsonLayout.createDefaultLayout(); | ||
Property[] headers = new Property[] { | ||
Property.createProperty("Header1", "Value1"), Property.createProperty("Header2", "Value2") | ||
}; | ||
|
||
HttpAppender.Builder<?> builder = | ||
getBuilder().setUrl(url).setLayout(layout).setHeaders(headers); | ||
|
||
HttpAppender appender = builder.build(); | ||
assertNotNull(appender, "HttpAppender should be created with headers."); | ||
} | ||
|
||
@Test | ||
void testBuilderWithSslConfiguration() throws Exception { | ||
URL url = new URL("https://example.com"); | ||
Layout<?> layout = JsonLayout.createDefaultLayout(); | ||
SslConfiguration sslConfig = mock(SslConfiguration.class); | ||
|
||
HttpAppender.Builder<?> builder = | ||
getBuilder().setUrl(url).setLayout(layout).setSslConfiguration(sslConfig); | ||
|
||
HttpAppender appender = builder.build(); | ||
assertNotNull(appender, "HttpAppender should be created with SSL configuration."); | ||
} | ||
|
||
@Test | ||
void testBuilderWithInvalidUrl() { | ||
assertThrows(MalformedURLException.class, () -> new URL("invalid-url")); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.