-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (jkube-kit/common) : Add HTTP utility methods to Fabric8HttpUtil
Add various methods required for performing HTTP requests to Fabric8HttpUtil. Use Fabric8 K8s Client's HttpResponse abstractions for HTTP requests. Signed-off-by: Rohan Kumar <[email protected]>
- Loading branch information
1 parent
0bf40bd
commit 1ac4f7c
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/Fabric8HttpUtil.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,71 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.common.util; | ||
|
||
import io.fabric8.kubernetes.client.http.HttpResponse; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.apache.commons.lang3.StringUtils.strip; | ||
|
||
public class Fabric8HttpUtil { | ||
private static final String WWW_AUTHENTICATE = "WWW-Authenticate"; | ||
|
||
private Fabric8HttpUtil() { } | ||
|
||
/** | ||
* Parse WWW-Authenticate Header as map | ||
* | ||
* @param response Http Response of a particular request | ||
* @return map containing various components of header as key value pairs | ||
*/ | ||
public static Map<String, String> extractAuthenticationChallengeIntoMap(HttpResponse<byte[]> response) { | ||
String wwwAuthenticateHeader = response.header(WWW_AUTHENTICATE); | ||
String[] wwwAuthenticateHeaders = wwwAuthenticateHeader.split(","); | ||
Map<String, String> result = new HashMap<>(); | ||
for (String challenge : wwwAuthenticateHeaders) { | ||
if (challenge.contains("=")) { | ||
String[] challengeParts = challenge.split("="); | ||
if (challengeParts.length == 2) { | ||
result.put(challengeParts[0], strip(challengeParts[1], "\"")); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
|
||
/** | ||
* Create Form Data String from map | ||
* | ||
* @param formData map containing key value pairs for form data | ||
* @return URL encoded value of form data | ||
*/ | ||
public static String toFormData(Map<String, String> formData) throws UnsupportedEncodingException { | ||
StringBuilder result = new StringBuilder(); | ||
for (Map.Entry<String, String> e : formData.entrySet()) { | ||
if (result.length() > 0) { | ||
result.append("&"); | ||
} | ||
result.append(URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8.name())); | ||
result.append("="); | ||
result.append(URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8.name())); | ||
} | ||
return result.toString(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/Fabric8HttpUtilTest.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,68 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.common.util; | ||
|
||
import io.fabric8.kubernetes.client.http.HttpResponse; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.net.HttpURLConnection.HTTP_OK; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class Fabric8HttpUtilTest { | ||
|
||
@Test | ||
void toFormData_whenDataProvidedAsMap_thenCreateFormDataPayload() throws UnsupportedEncodingException { | ||
// Given | ||
Map<String, String> formDataMap = new HashMap<>(); | ||
formDataMap.put("grant_type", "password"); | ||
formDataMap.put("refresh_token", "secret"); | ||
formDataMap.put("service", "auth.example.com"); | ||
formDataMap.put("scope", "repository=myuser/test-chart:pull"); | ||
formDataMap.put("client id", "Eclipse&JKube"); | ||
formDataMap.put("username", "?myuser"); | ||
formDataMap.put("password", "secret"); | ||
|
||
// When | ||
String formDataPayload = Fabric8HttpUtil.toFormData(formDataMap); | ||
|
||
// Then | ||
assertThat(formDataPayload) | ||
.isEqualTo("client+id=Eclipse%26JKube&refresh_token=secret&password=secret&grant_type=password&service=auth.example.com&scope=repository%3Dmyuser%2Ftest-chart%3Apull&username=%3Fmyuser"); | ||
} | ||
|
||
@Test | ||
void extractAuthenticationChallengeIntoMap_whenWwwHeaderProvided_thenParseDataIntoMap() { | ||
// Given | ||
String wwwAuthenticateValue = "Bearer realm=\"https://auth.example.com/token\",service=\"registry.example.com\",scope=\"repository:myuser/test-chart:pull\""; | ||
Map<String, List<String>> responseHeaders = new HashMap<>(); | ||
responseHeaders.put("WWW-Authenticate", Collections.singletonList(wwwAuthenticateValue)); | ||
HttpResponse<byte[]> response = new TestFabric8HttpResponse(HTTP_OK, responseHeaders, null, null); | ||
|
||
// When | ||
Map<String, String> wwwAuthenticateAsMap = Fabric8HttpUtil.extractAuthenticationChallengeIntoMap(response); | ||
|
||
// Then | ||
assertThat(wwwAuthenticateAsMap) | ||
.hasSize(3) | ||
.containsEntry("Bearer realm", "https://auth.example.com/token") | ||
.containsEntry("service", "registry.example.com") | ||
.containsEntry("scope", "repository:myuser/test-chart:pull"); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...e-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/TestFabric8HttpResponse.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,62 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.common.util; | ||
|
||
import io.fabric8.kubernetes.client.http.HttpRequest; | ||
import io.fabric8.kubernetes.client.http.HttpResponse; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class TestFabric8HttpResponse implements HttpResponse<byte[]> { | ||
private final int code; | ||
private final Map<String, List<String>> headers; | ||
private final String body; | ||
private final String message; | ||
|
||
public TestFabric8HttpResponse(int code, Map<String, List<String>> headers, String body, String message) { | ||
this.code = code; | ||
this.headers = headers; | ||
this.body = body; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public int code() { return code; } | ||
|
||
@Override | ||
public byte[] body() { | ||
if (StringUtils.isNotBlank(body)) { | ||
return body.getBytes(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public HttpRequest request() { return null; } | ||
|
||
@Override | ||
public Optional<HttpResponse<?>> previousResponse() { return Optional.empty(); } | ||
|
||
@Override | ||
public List<String> headers(String s) { return headers.get(s); } | ||
|
||
@Override | ||
public Map<String, List<String>> headers() { return headers; } | ||
|
||
@Override | ||
public String message() { return message; } | ||
} |