gnodet-bot commented on code in PR #27614:
URL: https://github.com/apache/camel/pull/27614#discussion_r4228902659


##########
components/camel-http/src/test/java/org/apache/camel/component/http/HttpOAuth2ResourceIndicatorTest.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.camel.component.http;
+
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.http.handler.HeaderValidationHandler;
+import org.apache.camel.util.json.Jsoner;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.net.WWWFormCodec;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The token request of the OAuth2 client credentials flow with a resource 
indicator (RFC 8707) has the resource
+ * parameter besides the other parameters.
+ */
+public class HttpOAuth2ResourceIndicatorTest extends BaseHttpTest {
+
+    private static final String FAKE_TOKEN = "xxx.yyy.zzz";
+    private static final String CLIENT_ID = "test-client";
+    private static final String CLIENT_SECRET = "test-secret";
+    private static final String SCOPE = "test-scope";
+    private static final String RESOURCE = "https://api.example.com/";;
+
+    // the parameters of the token requests the token endpoint received
+    private final List<Map<String, String>> tokenRequests = new 
CopyOnWriteArrayList<>();
+
+    @Override
+    public void setupResources() throws Exception {
+    }
+
+    @Test
+    public void resourceIsAddedToTheTokenRequest() throws Exception {
+        Map<String, String> tokenRequest = requestWithToken(false);
+
+        assertEquals("client_credentials", tokenRequest.get("grant_type"));
+        assertEquals(SCOPE, tokenRequest.get("scope"));
+        assertEquals(RESOURCE, tokenRequest.get("resource"));
+    }
+
+    @Test
+    public void resourceIsAddedToTheTokenRequestWithBodyAuthentication() 
throws Exception {
+        Map<String, String> tokenRequest = requestWithToken(true);
+
+        assertEquals("client_credentials", tokenRequest.get("grant_type"));
+        assertEquals(SCOPE, tokenRequest.get("scope"));
+        assertEquals(CLIENT_ID, tokenRequest.get("client_id"));
+        assertEquals(CLIENT_SECRET, tokenRequest.get("client_secret"));
+        assertEquals(RESOURCE, tokenRequest.get("resource"));
+    }
+
+    // Sends a request that needs a token, and returns the parameters of the 
token request
+    private Map<String, String> requestWithToken(boolean bodyAuthentication) 
throws Exception {
+        tokenRequests.clear();
+        try (HttpServer localServer = createLocalServer()) {
+            String tokenEndpoint = "http://localhost:"; + 
localServer.getLocalPort() + "/token";
+            String requestUrl = "http://localhost:"; + 
localServer.getLocalPort() + "/post?httpMethod=POST&oauth2ClientId="

Review Comment:
   💡 `RESOURCE = "https://api.example.com/"` is concatenated raw into the query 
string without URL-encoding. The colons and slashes happen to be safe in this 
test, but if a future test uses a resource URI with `+`, `=`, `&`, or `%`, the 
URL would be malformed. Consider `URLEncoder.encode(RESOURCE, 
StandardCharsets.UTF_8)` for robustness.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to