NIFI-7605 Removed user-agent default value so no header will be sent by default.

Added and updated unit tests.

This closes #4428.

Signed-off-by: Andy LoPresto <alopresto@apache.org>
Signed-off-by: Joe Witt <joe.witt@gmail.com>
This commit is contained in:
Mike Thomsen 2020-07-24 07:52:27 -04:00 committed by Andy LoPresto
parent c980b64bf5
commit 0861b2f632
No known key found for this signature in database
GPG Key ID: 6EC293152D90B61D
2 changed files with 129 additions and 100 deletions

View File

@ -254,7 +254,6 @@ public class InvokeHTTP extends AbstractProcessor {
.displayName("Useragent") .displayName("Useragent")
.description("The Useragent identifier sent along with each request") .description("The Useragent identifier sent along with each request")
.required(false) .required(false)
.defaultValue("Apache Nifi/${nifi.version} (git:${nifi.build.git.commit.id.describe}; https://nifi.apache.org/)")
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build(); .build();
@ -1046,9 +1045,7 @@ public class InvokeHTTP extends AbstractProcessor {
} }
String userAgent = trimToEmpty(context.getProperty(PROP_USERAGENT).evaluateAttributeExpressions(requestFlowFile).getValue()); String userAgent = trimToEmpty(context.getProperty(PROP_USERAGENT).evaluateAttributeExpressions(requestFlowFile).getValue());
if (!userAgent.isEmpty()) {
requestBuilder.addHeader("User-Agent", userAgent); requestBuilder.addHeader("User-Agent", userAgent);
}
requestBuilder = setHeaderProperties(context, requestBuilder, requestFlowFile); requestBuilder = setHeaderProperties(context, requestBuilder, requestFlowFile);
@ -1316,7 +1313,7 @@ public class InvokeHTTP extends AbstractProcessor {
* Retrieve the directory in which OkHttp should cache responses. This method opts * Retrieve the directory in which OkHttp should cache responses. This method opts
* to use a temp directory to write the cache, which means that the cache will be written * to use a temp directory to write the cache, which means that the cache will be written
* to a new location each time this processor is scheduled. * to a new location each time this processor is scheduled.
* * <p>
* Ref: https://github.com/square/okhttp/wiki/Recipes#response-caching * Ref: https://github.com/square/okhttp/wiki/Recipes#response-caching
* *
* @return the directory in which the ETag cache should be written * @return the directory in which the ETag cache should be written

View File

@ -16,10 +16,12 @@
*/ */
package org.apache.nifi.processors.standard; package org.apache.nifi.processors.standard;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -46,11 +48,11 @@ import org.junit.Assume;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger;
import static java.nio.charset.StandardCharsets.UTF_8; import org.slf4j.LoggerFactory;
import static org.junit.Assert.assertTrue;
public class TestInvokeHTTP extends TestInvokeHttpCommon { public class TestInvokeHTTP extends TestInvokeHttpCommon {
private static final Logger logger = LoggerFactory.getLogger(TestInvokeHTTP.class);
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
@ -345,13 +347,43 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon {
} }
@Test @Test
public void testUserAgent() throws Exception { public void testShouldNotSendUserAgentByDefault() throws Exception {
addHandler(new EchoUseragentHandler()); // Arrange
addHandler(new EchoUserAgentHandler());
runner.setProperty(InvokeHTTP.PROP_URL, url); runner.setProperty(InvokeHTTP.PROP_URL, url);
createFlowFiles(runner); createFlowFiles(runner);
// Act
runner.run();
// Assert
runner.assertTransferCount(InvokeHTTP.REL_SUCCESS_REQ, 1);
runner.assertTransferCount(InvokeHTTP.REL_RESPONSE, 1);
runner.assertTransferCount(InvokeHTTP.REL_RETRY, 0);
runner.assertTransferCount(InvokeHTTP.REL_NO_RETRY, 0);
runner.assertTransferCount(InvokeHTTP.REL_FAILURE, 0);
runner.assertPenalizeCount(0);
final MockFlowFile response = runner.getFlowFilesForRelationship(InvokeHTTP.REL_RESPONSE).get(0);
String content = new String(response.toByteArray(), UTF_8);
logger.info("Returned flowfile content: " + content);
assertTrue(content.isEmpty());
response.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200");
response.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK");
}
@Test
public void testShouldSetUserAgentExplicitly() throws Exception {
addHandler(new EchoUserAgentHandler());
runner.setProperty(InvokeHTTP.PROP_USERAGENT, "Apache NiFi/${nifi.version} (git:${nifi.build.git.commit.id.describe}; https://nifi.apache.org/)");
runner.setProperty(InvokeHTTP.PROP_URL, url);
createFlowFiles(runner);
runner.run(); runner.run();
runner.assertTransferCount(InvokeHTTP.REL_SUCCESS_REQ, 1); runner.assertTransferCount(InvokeHTTP.REL_SUCCESS_REQ, 1);
@ -363,7 +395,7 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon {
final MockFlowFile response = runner.getFlowFilesForRelationship(InvokeHTTP.REL_RESPONSE).get(0); final MockFlowFile response = runner.getFlowFilesForRelationship(InvokeHTTP.REL_RESPONSE).get(0);
String content = new String(response.toByteArray(), UTF_8); String content = new String(response.toByteArray(), UTF_8);
assertTrue(content.startsWith("Apache Nifi/" + NifiBuildProperties.NIFI_VERSION + " (")); assertTrue(content.startsWith("Apache NiFi/" + NifiBuildProperties.NIFI_VERSION + " ("));
assertFalse("Missing expression language variables: " + content, content.contains("; ;")); assertFalse("Missing expression language variables: " + content, content.contains("; ;"));
response.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200"); response.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200");
@ -371,8 +403,8 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon {
} }
@Test @Test
public void testUserAgentChanged() throws Exception { public void testShouldSetUserAgentWithExpressionLanguage() throws Exception {
addHandler(new EchoUseragentHandler()); addHandler(new EchoUserAgentHandler());
runner.setProperty(InvokeHTTP.PROP_URL, url); runner.setProperty(InvokeHTTP.PROP_URL, url);
runner.setProperty(InvokeHTTP.PROP_USERAGENT, "${literal('And now for something completely different...')}"); runner.setProperty(InvokeHTTP.PROP_USERAGENT, "${literal('And now for something completely different...')}");
@ -396,7 +428,7 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon {
response.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK"); response.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK");
} }
public static class EchoUseragentHandler extends AbstractHandler { public static class EchoUserAgentHandler extends AbstractHandler {
@Override @Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {