forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HttpAuthorization added to azure-core-experimental (Azure#22370)
* HttpAuthorization added to azure-core-experimental * feedback and CI Co-authored-by: jschrepp-MSFT <[email protected]>
- Loading branch information
1 parent
0fd8cfd
commit 045d0bf
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...e-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.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,59 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.core.experimental.http; | ||
|
||
import com.azure.core.annotation.Immutable; | ||
import com.azure.core.util.logging.ClientLogger; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Represents the value of an HTTP Authorization header. | ||
*/ | ||
@Immutable | ||
public final class HttpAuthorization { | ||
private final ClientLogger logger = new ClientLogger(HttpAuthorization.class); | ||
private final String scheme; | ||
private final String parameter; | ||
|
||
/** | ||
* Constructs a new HttpAuthorization instance. | ||
* | ||
* @param scheme Scheme component of an authorization header value. | ||
* @param parameter The credentials used for the authorization header value. | ||
* @throws NullPointerException if any argument is null. | ||
* @throws IllegalArgumentException if any argument is an empty string. | ||
*/ | ||
public HttpAuthorization(String scheme, String parameter) { | ||
Objects.requireNonNull(scheme); | ||
Objects.requireNonNull(parameter); | ||
if (scheme.isEmpty()) { | ||
throw logger.logExceptionAsError(new IllegalArgumentException("scheme must be a nonempty string.")); | ||
} | ||
if (parameter.isEmpty()) { | ||
throw logger.logExceptionAsError(new IllegalArgumentException("parameter must be a nonempty string.")); | ||
} | ||
this.scheme = scheme; | ||
this.parameter = parameter; | ||
} | ||
|
||
/** | ||
* @return Scheme of the authorization header. | ||
*/ | ||
public String getScheme() { | ||
return scheme; | ||
} | ||
|
||
/** | ||
* @return Credential of the authorization header. | ||
*/ | ||
public String getParameter() { | ||
return parameter; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s %s", scheme, parameter); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...e-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.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,31 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.core.experimental.http; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link DynamicRequest}. | ||
*/ | ||
public class HttpAuthorizationTests { | ||
@Test | ||
public void nullOrWhiteSpaceParameters() | ||
{ | ||
Assertions.assertThrows(NullPointerException.class, () -> new HttpAuthorization(null, "parameter")); | ||
Assertions.assertThrows(NullPointerException.class, () -> new HttpAuthorization("scheme", null)); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpAuthorization("", "parameter")); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpAuthorization("scheme", "")); | ||
} | ||
|
||
@Test | ||
public void toStringTest() | ||
{ | ||
String scheme = "scheme"; | ||
String parameter = "parameter"; | ||
HttpAuthorization httpAuthorization = new HttpAuthorization(scheme, parameter); | ||
|
||
Assertions.assertEquals(String.format("%s %s", scheme, parameter), httpAuthorization.toString()); | ||
} | ||
} |