mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-04 09:26:11 +00:00
[Java][resttemplate] Add test for bearer auth (#17081)
* add bearer auth API to echo-api * run generate-samples.sh * add resttemplate echo-api sample * add bearer auth test * remove @Ignore
This commit is contained in:
@@ -106,6 +106,8 @@ Class | Method | HTTP request | Description
|
||||
------------ | ------------- | ------------- | -------------
|
||||
*AuthApi* | [**testAuthHttpBasic**](docs/AuthApi.md#testAuthHttpBasic) | **POST** /auth/http/basic | To test HTTP basic authentication
|
||||
*AuthApi* | [**testAuthHttpBasicWithHttpInfo**](docs/AuthApi.md#testAuthHttpBasicWithHttpInfo) | **POST** /auth/http/basic | To test HTTP basic authentication
|
||||
*AuthApi* | [**testAuthHttpBearer**](docs/AuthApi.md#testAuthHttpBearer) | **POST** /auth/http/bearer | To test HTTP bearer authentication
|
||||
*AuthApi* | [**testAuthHttpBearerWithHttpInfo**](docs/AuthApi.md#testAuthHttpBearerWithHttpInfo) | **POST** /auth/http/bearer | To test HTTP bearer authentication
|
||||
*BodyApi* | [**testBinaryGif**](docs/BodyApi.md#testBinaryGif) | **POST** /binary/gif | Test binary (gif) response body
|
||||
*BodyApi* | [**testBinaryGifWithHttpInfo**](docs/BodyApi.md#testBinaryGifWithHttpInfo) | **POST** /binary/gif | Test binary (gif) response body
|
||||
*BodyApi* | [**testBodyApplicationOctetstreamBinary**](docs/BodyApi.md#testBodyApplicationOctetstreamBinary) | **POST** /body/application/octetstream/binary | Test body parameter(s)
|
||||
@@ -174,6 +176,12 @@ Authentication schemes defined for the API:
|
||||
|
||||
- **Type**: HTTP basic authentication
|
||||
|
||||
<a id="http_bearer_auth"></a>
|
||||
### http_bearer_auth
|
||||
|
||||
|
||||
- **Type**: HTTP Bearer Token authentication
|
||||
|
||||
|
||||
## Recommendation
|
||||
|
||||
|
||||
@@ -551,6 +551,23 @@ paths:
|
||||
tags:
|
||||
- auth
|
||||
x-accepts: text/plain
|
||||
/auth/http/bearer:
|
||||
post:
|
||||
description: To test HTTP bearer authentication
|
||||
operationId: test/auth/http/bearer
|
||||
responses:
|
||||
"200":
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
type: string
|
||||
description: Successful operation
|
||||
security:
|
||||
- http_bearer_auth: []
|
||||
summary: To test HTTP bearer authentication
|
||||
tags:
|
||||
- auth
|
||||
x-accepts: text/plain
|
||||
components:
|
||||
requestBodies:
|
||||
Pet:
|
||||
@@ -822,4 +839,7 @@ components:
|
||||
http_auth:
|
||||
scheme: basic
|
||||
type: http
|
||||
http_bearer_auth:
|
||||
scheme: bearer
|
||||
type: http
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ All URIs are relative to *http://localhost:3000*
|
||||
|------------- | ------------- | -------------|
|
||||
| [**testAuthHttpBasic**](AuthApi.md#testAuthHttpBasic) | **POST** /auth/http/basic | To test HTTP basic authentication |
|
||||
| [**testAuthHttpBasicWithHttpInfo**](AuthApi.md#testAuthHttpBasicWithHttpInfo) | **POST** /auth/http/basic | To test HTTP basic authentication |
|
||||
| [**testAuthHttpBearer**](AuthApi.md#testAuthHttpBearer) | **POST** /auth/http/bearer | To test HTTP bearer authentication |
|
||||
| [**testAuthHttpBearerWithHttpInfo**](AuthApi.md#testAuthHttpBearerWithHttpInfo) | **POST** /auth/http/bearer | To test HTTP bearer authentication |
|
||||
|
||||
|
||||
|
||||
@@ -146,3 +148,139 @@ ApiResponse<**String**>
|
||||
|-------------|-------------|------------------|
|
||||
| **200** | Successful operation | - |
|
||||
|
||||
|
||||
## testAuthHttpBearer
|
||||
|
||||
> String testAuthHttpBearer()
|
||||
|
||||
To test HTTP bearer authentication
|
||||
|
||||
To test HTTP bearer authentication
|
||||
|
||||
### Example
|
||||
|
||||
```java
|
||||
// Import classes:
|
||||
import org.openapitools.client.ApiClient;
|
||||
import org.openapitools.client.ApiException;
|
||||
import org.openapitools.client.Configuration;
|
||||
import org.openapitools.client.auth.*;
|
||||
import org.openapitools.client.models.*;
|
||||
import org.openapitools.client.api.AuthApi;
|
||||
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
||||
defaultClient.setBasePath("http://localhost:3000");
|
||||
|
||||
// Configure HTTP bearer authorization: http_bearer_auth
|
||||
HttpBearerAuth http_bearer_auth = (HttpBearerAuth) defaultClient.getAuthentication("http_bearer_auth");
|
||||
http_bearer_auth.setBearerToken("BEARER TOKEN");
|
||||
|
||||
AuthApi apiInstance = new AuthApi(defaultClient);
|
||||
try {
|
||||
String result = apiInstance.testAuthHttpBearer();
|
||||
System.out.println(result);
|
||||
} catch (ApiException e) {
|
||||
System.err.println("Exception when calling AuthApi#testAuthHttpBearer");
|
||||
System.err.println("Status code: " + e.getCode());
|
||||
System.err.println("Reason: " + e.getResponseBody());
|
||||
System.err.println("Response headers: " + e.getResponseHeaders());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
This endpoint does not need any parameter.
|
||||
|
||||
### Return type
|
||||
|
||||
**String**
|
||||
|
||||
|
||||
### Authorization
|
||||
|
||||
[http_bearer_auth](../README.md#http_bearer_auth)
|
||||
|
||||
### HTTP request headers
|
||||
|
||||
- **Content-Type**: Not defined
|
||||
- **Accept**: text/plain
|
||||
|
||||
### HTTP response details
|
||||
| Status code | Description | Response headers |
|
||||
|-------------|-------------|------------------|
|
||||
| **200** | Successful operation | - |
|
||||
|
||||
## testAuthHttpBearerWithHttpInfo
|
||||
|
||||
> ApiResponse<String> testAuthHttpBearer testAuthHttpBearerWithHttpInfo()
|
||||
|
||||
To test HTTP bearer authentication
|
||||
|
||||
To test HTTP bearer authentication
|
||||
|
||||
### Example
|
||||
|
||||
```java
|
||||
// Import classes:
|
||||
import org.openapitools.client.ApiClient;
|
||||
import org.openapitools.client.ApiException;
|
||||
import org.openapitools.client.ApiResponse;
|
||||
import org.openapitools.client.Configuration;
|
||||
import org.openapitools.client.auth.*;
|
||||
import org.openapitools.client.models.*;
|
||||
import org.openapitools.client.api.AuthApi;
|
||||
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
||||
defaultClient.setBasePath("http://localhost:3000");
|
||||
|
||||
// Configure HTTP bearer authorization: http_bearer_auth
|
||||
HttpBearerAuth http_bearer_auth = (HttpBearerAuth) defaultClient.getAuthentication("http_bearer_auth");
|
||||
http_bearer_auth.setBearerToken("BEARER TOKEN");
|
||||
|
||||
AuthApi apiInstance = new AuthApi(defaultClient);
|
||||
try {
|
||||
ApiResponse<String> response = apiInstance.testAuthHttpBearerWithHttpInfo();
|
||||
System.out.println("Status code: " + response.getStatusCode());
|
||||
System.out.println("Response headers: " + response.getHeaders());
|
||||
System.out.println("Response body: " + response.getData());
|
||||
} catch (ApiException e) {
|
||||
System.err.println("Exception when calling AuthApi#testAuthHttpBearer");
|
||||
System.err.println("Status code: " + e.getCode());
|
||||
System.err.println("Response headers: " + e.getResponseHeaders());
|
||||
System.err.println("Reason: " + e.getResponseBody());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
This endpoint does not need any parameter.
|
||||
|
||||
### Return type
|
||||
|
||||
ApiResponse<**String**>
|
||||
|
||||
|
||||
### Authorization
|
||||
|
||||
[http_bearer_auth](../README.md#http_bearer_auth)
|
||||
|
||||
### HTTP request headers
|
||||
|
||||
- **Content-Type**: Not defined
|
||||
- **Accept**: text/plain
|
||||
|
||||
### HTTP response details
|
||||
| Status code | Description | Response headers |
|
||||
|-------------|-------------|------------------|
|
||||
| **200** | Successful operation | - |
|
||||
|
||||
|
||||
@@ -144,6 +144,79 @@ public class AuthApi {
|
||||
|
||||
localVarRequestBuilder.header("Accept", "text/plain");
|
||||
|
||||
localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
|
||||
if (memberVarReadTimeout != null) {
|
||||
localVarRequestBuilder.timeout(memberVarReadTimeout);
|
||||
}
|
||||
if (memberVarInterceptor != null) {
|
||||
memberVarInterceptor.accept(localVarRequestBuilder);
|
||||
}
|
||||
return localVarRequestBuilder;
|
||||
}
|
||||
/**
|
||||
* To test HTTP bearer authentication
|
||||
* To test HTTP bearer authentication
|
||||
* @return String
|
||||
* @throws ApiException if fails to make API call
|
||||
*/
|
||||
public String testAuthHttpBearer() throws ApiException {
|
||||
ApiResponse<String> localVarResponse = testAuthHttpBearerWithHttpInfo();
|
||||
return localVarResponse.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* To test HTTP bearer authentication
|
||||
* To test HTTP bearer authentication
|
||||
* @return ApiResponse<String>
|
||||
* @throws ApiException if fails to make API call
|
||||
*/
|
||||
public ApiResponse<String> testAuthHttpBearerWithHttpInfo() throws ApiException {
|
||||
HttpRequest.Builder localVarRequestBuilder = testAuthHttpBearerRequestBuilder();
|
||||
try {
|
||||
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
|
||||
localVarRequestBuilder.build(),
|
||||
HttpResponse.BodyHandlers.ofInputStream());
|
||||
if (memberVarResponseInterceptor != null) {
|
||||
memberVarResponseInterceptor.accept(localVarResponse);
|
||||
}
|
||||
try {
|
||||
if (localVarResponse.statusCode()/ 100 != 2) {
|
||||
throw getApiException("testAuthHttpBearer", localVarResponse);
|
||||
}
|
||||
// for plain text response
|
||||
if (localVarResponse.headers().map().containsKey("Content-Type") &&
|
||||
"text/plain".equalsIgnoreCase(localVarResponse.headers().map().get("Content-Type").get(0).split(";")[0].trim())) {
|
||||
java.util.Scanner s = new java.util.Scanner(localVarResponse.body()).useDelimiter("\\A");
|
||||
String responseBodyText = s.hasNext() ? s.next() : "";
|
||||
return new ApiResponse<String>(
|
||||
localVarResponse.statusCode(),
|
||||
localVarResponse.headers().map(),
|
||||
responseBodyText
|
||||
);
|
||||
} else {
|
||||
throw new RuntimeException("Error! The response Content-Type is supposed to be `text/plain` but it's not: " + localVarResponse);
|
||||
}
|
||||
} finally {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ApiException(e);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new ApiException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpRequest.Builder testAuthHttpBearerRequestBuilder() throws ApiException {
|
||||
|
||||
HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();
|
||||
|
||||
String localVarPath = "/auth/http/bearer";
|
||||
|
||||
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
|
||||
|
||||
localVarRequestBuilder.header("Accept", "text/plain");
|
||||
|
||||
localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
|
||||
if (memberVarReadTimeout != null) {
|
||||
localVarRequestBuilder.timeout(memberVarReadTimeout);
|
||||
|
||||
Reference in New Issue
Block a user