Skip to content

Commit

Permalink
HttpAuthorization added to azure-core-experimental (Azure#22370)
Browse files Browse the repository at this point in the history
* HttpAuthorization added to azure-core-experimental

* feedback and CI

Co-authored-by: jschrepp-MSFT <[email protected]>
  • Loading branch information
jaschrep-msft and jaschrep-msft authored Jun 18, 2021
1 parent 0fd8cfd commit 045d0bf
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
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);
}
}
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());
}
}

0 comments on commit 045d0bf

Please sign in to comment.