You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2022/11/16 15:29:15 UTC

[GitHub] [sling-org-apache-sling-testing-clients] Kronnox commented on a diff in pull request #41: SLING-XXX: Set more descriptive User-Agent for SlingClients

Kronnox commented on code in PR #41:
URL: https://github.com/apache/sling-org-apache-sling-testing-clients/pull/41#discussion_r1024158765


##########
src/test/java/org/apache/sling/testing/CustomUserAgentInterceptorTest.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.sling.testing;
+
+import org.apache.http.entity.StringEntity;
+import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.HttpServerRule;
+import org.apache.sling.testing.clients.SlingClient;
+import org.apache.sling.testing.clients.SlingHttpResponse;
+import org.apache.sling.testing.clients.interceptors.UserAgentHolder;
+import org.apache.sling.testing.clients.interceptors.UserAgentInterceptor;
+import org.apache.sling.testing.clients.util.UserAgent;
+import org.junit.*;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class CustomUserAgentInterceptorTest {
+
+    private static final String PATH = "/mirror";
+
+    private static final String USER_AGENT_HEADER = "User-Agent";
+    private static final String MODIFIED_AGENT = "modified-agent";
+    private static final String CUSTOM_AGENT_TITLE = "test-client";
+    private static final String CUSTOM_AGENT_VERSION = "1.2.3";
+    private static final String CUSTOM_AGENT_DETAILS = "details";
+    private static final String CUSTOM_AGENT = String.format("%s/%s", CUSTOM_AGENT_TITLE, CUSTOM_AGENT_VERSION);
+    private static final String CUSTOM_AGENT_WITH_DETAILS = String.format("%s/%s (%s)", CUSTOM_AGENT_TITLE, CUSTOM_AGENT_VERSION, CUSTOM_AGENT_DETAILS);
+    private static final String CUSTOM_AGENT_WITH_APPEND = String.format("%s/%s %s", CUSTOM_AGENT_TITLE, CUSTOM_AGENT_VERSION, CUSTOM_AGENT_DETAILS);
+
+    @ClassRule
+    public static HttpServerRule httpServer = new HttpServerRule() {
+        @Override
+        protected void registerHandlers() {
+            serverBootstrap.registerHandler(PATH, (request, response, context) -> {
+                response.setEntity(new StringEntity("Success!"));
+                response.setStatusCode(200);
+                response.setHeaders(request.getHeaders(USER_AGENT_HEADER));
+            });
+        }
+    };
+    private static SlingClient client;
+
+    @BeforeClass
+    public static void beforeClass() throws ClientException {
+        client = SlingClient.Builder
+                .create(httpServer.getURI(), "user", "pass")
+                .addInterceptorLast(new UserAgentInterceptor())
+                .build();
+    }
+
+    @After
+    public void after() {
+        UserAgentHolder.reset();
+    }
+
+    @Test
+    public void testDefault() throws ClientException {
+        SlingHttpResponse response = client.doGet(PATH, 200);

Review Comment:
   I totally agree



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org