Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpAuthorization added to azure-core-experimental #22370

Merged
merged 2 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
*/
srnagar marked this conversation as resolved.
Show resolved Hide resolved
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());
}
}