forked from loafle/openapi-generator-original
[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:
@@ -75,6 +75,9 @@ class AuthApi
|
||||
'testAuthHttpBasic' => [
|
||||
'application/json',
|
||||
],
|
||||
'testAuthHttpBearer' => [
|
||||
'application/json',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -400,6 +403,283 @@ class AuthApi
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation testAuthHttpBearer
|
||||
*
|
||||
* To test HTTP bearer authentication
|
||||
*
|
||||
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBearer'] to see the possible values for this operation
|
||||
*
|
||||
* @throws ApiException on non-2xx response
|
||||
* @throws InvalidArgumentException
|
||||
* @return string
|
||||
*/
|
||||
public function testAuthHttpBearer(
|
||||
string $contentType = self::contentTypes['testAuthHttpBearer'][0]
|
||||
): string
|
||||
{
|
||||
list($response) = $this->testAuthHttpBearerWithHttpInfo($contentType);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation testAuthHttpBearerWithHttpInfo
|
||||
*
|
||||
* To test HTTP bearer authentication
|
||||
*
|
||||
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBearer'] to see the possible values for this operation
|
||||
*
|
||||
* @throws ApiException on non-2xx response
|
||||
* @throws InvalidArgumentException
|
||||
* @return array of string, HTTP status code, HTTP response headers (array of strings)
|
||||
*/
|
||||
public function testAuthHttpBearerWithHttpInfo(
|
||||
string $contentType = self::contentTypes['testAuthHttpBearer'][0]
|
||||
): array
|
||||
{
|
||||
$request = $this->testAuthHttpBearerRequest($contentType);
|
||||
|
||||
try {
|
||||
$options = $this->createHttpClientOption();
|
||||
try {
|
||||
$response = $this->client->send($request, $options);
|
||||
} catch (RequestException $e) {
|
||||
throw new ApiException(
|
||||
"[{$e->getCode()}] {$e->getMessage()}",
|
||||
(int) $e->getCode(),
|
||||
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
|
||||
$e->getResponse() ? (string) $e->getResponse()->getBody() : null
|
||||
);
|
||||
} catch (ConnectException $e) {
|
||||
throw new ApiException(
|
||||
"[{$e->getCode()}] {$e->getMessage()}",
|
||||
(int) $e->getCode(),
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
|
||||
if ($statusCode < 200 || $statusCode > 299) {
|
||||
throw new ApiException(
|
||||
sprintf(
|
||||
'[%d] Error connecting to the API (%s)',
|
||||
$statusCode,
|
||||
(string) $request->getUri()
|
||||
),
|
||||
$statusCode,
|
||||
$response->getHeaders(),
|
||||
(string) $response->getBody()
|
||||
);
|
||||
}
|
||||
|
||||
switch($statusCode) {
|
||||
case 200:
|
||||
if ('string' === '\SplFileObject') {
|
||||
$content = $response->getBody(); //stream goes to serializer
|
||||
} else {
|
||||
$content = (string) $response->getBody();
|
||||
if ('string' !== 'string') {
|
||||
$content = json_decode($content);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
ObjectSerializer::deserialize($content, 'string', []),
|
||||
$response->getStatusCode(),
|
||||
$response->getHeaders()
|
||||
];
|
||||
}
|
||||
|
||||
$returnType = 'string';
|
||||
if ($returnType === '\SplFileObject') {
|
||||
$content = $response->getBody(); //stream goes to serializer
|
||||
} else {
|
||||
$content = (string) $response->getBody();
|
||||
if ($returnType !== 'string') {
|
||||
$content = json_decode($content);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
ObjectSerializer::deserialize($content, $returnType, []),
|
||||
$response->getStatusCode(),
|
||||
$response->getHeaders()
|
||||
];
|
||||
|
||||
} catch (ApiException $e) {
|
||||
switch ($e->getCode()) {
|
||||
case 200:
|
||||
$data = ObjectSerializer::deserialize(
|
||||
$e->getResponseBody(),
|
||||
'string',
|
||||
$e->getResponseHeaders()
|
||||
);
|
||||
$e->setResponseObject($data);
|
||||
break;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation testAuthHttpBearerAsync
|
||||
*
|
||||
* To test HTTP bearer authentication
|
||||
*
|
||||
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBearer'] to see the possible values for this operation
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function testAuthHttpBearerAsync(
|
||||
string $contentType = self::contentTypes['testAuthHttpBearer'][0]
|
||||
): PromiseInterface
|
||||
{
|
||||
return $this->testAuthHttpBearerAsyncWithHttpInfo($contentType)
|
||||
->then(
|
||||
function ($response) {
|
||||
return $response[0];
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Operation testAuthHttpBearerAsyncWithHttpInfo
|
||||
*
|
||||
* To test HTTP bearer authentication
|
||||
*
|
||||
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBearer'] to see the possible values for this operation
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function testAuthHttpBearerAsyncWithHttpInfo(
|
||||
string $contentType = self::contentTypes['testAuthHttpBearer'][0]
|
||||
): PromiseInterface
|
||||
{
|
||||
$returnType = 'string';
|
||||
$request = $this->testAuthHttpBearerRequest($contentType);
|
||||
|
||||
return $this->client
|
||||
->sendAsync($request, $this->createHttpClientOption())
|
||||
->then(
|
||||
function ($response) use ($returnType) {
|
||||
if ($returnType === '\SplFileObject') {
|
||||
$content = $response->getBody(); //stream goes to serializer
|
||||
} else {
|
||||
$content = (string) $response->getBody();
|
||||
if ($returnType !== 'string') {
|
||||
$content = json_decode($content);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
ObjectSerializer::deserialize($content, $returnType, []),
|
||||
$response->getStatusCode(),
|
||||
$response->getHeaders()
|
||||
];
|
||||
},
|
||||
function ($exception) {
|
||||
$response = $exception->getResponse();
|
||||
$statusCode = $response->getStatusCode();
|
||||
throw new ApiException(
|
||||
sprintf(
|
||||
'[%d] Error connecting to the API (%s)',
|
||||
$statusCode,
|
||||
$exception->getRequest()->getUri()
|
||||
),
|
||||
$statusCode,
|
||||
$response->getHeaders(),
|
||||
(string) $response->getBody()
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create request for operation 'testAuthHttpBearer'
|
||||
*
|
||||
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testAuthHttpBearer'] to see the possible values for this operation
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @return \GuzzleHttp\Psr7\Request
|
||||
*/
|
||||
public function testAuthHttpBearerRequest(
|
||||
string $contentType = self::contentTypes['testAuthHttpBearer'][0]
|
||||
): Request
|
||||
{
|
||||
|
||||
|
||||
$resourcePath = '/auth/http/bearer';
|
||||
$formParams = [];
|
||||
$queryParams = [];
|
||||
$headerParams = [];
|
||||
$httpBody = '';
|
||||
$multipart = false;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$headers = $this->headerSelector->selectHeaders(
|
||||
['text/plain', ],
|
||||
$contentType,
|
||||
$multipart
|
||||
);
|
||||
|
||||
// for model (json/xml)
|
||||
if (count($formParams) > 0) {
|
||||
if ($multipart) {
|
||||
$multipartContents = [];
|
||||
foreach ($formParams as $formParamName => $formParamValue) {
|
||||
$formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
|
||||
foreach ($formParamValueItems as $formParamValueItem) {
|
||||
$multipartContents[] = [
|
||||
'name' => $formParamName,
|
||||
'contents' => $formParamValueItem
|
||||
];
|
||||
}
|
||||
}
|
||||
// for HTTP post (form)
|
||||
$httpBody = new MultipartStream($multipartContents);
|
||||
|
||||
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
|
||||
# if Content-Type contains "application/json", json_encode the form parameters
|
||||
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
|
||||
} else {
|
||||
// for HTTP post (form)
|
||||
$httpBody = ObjectSerializer::buildQuery($formParams);
|
||||
}
|
||||
}
|
||||
|
||||
// this endpoint requires Bearer authentication (access token)
|
||||
if (!empty($this->config->getAccessToken())) {
|
||||
$headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
|
||||
}
|
||||
|
||||
$defaultHeaders = [];
|
||||
if ($this->config->getUserAgent()) {
|
||||
$defaultHeaders['User-Agent'] = $this->config->getUserAgent();
|
||||
}
|
||||
|
||||
$headers = array_merge(
|
||||
$defaultHeaders,
|
||||
$headerParams,
|
||||
$headers
|
||||
);
|
||||
|
||||
$operationHost = $this->config->getHost();
|
||||
$query = ObjectSerializer::buildQuery($queryParams);
|
||||
return new Request(
|
||||
'POST',
|
||||
$operationHost . $resourcePath . ($query ? "?{$query}" : ''),
|
||||
$headers,
|
||||
$httpBody
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create http client option
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user