Skip to content

Commit

Permalink
Adding test for Unix time encoded values in path
Browse files Browse the repository at this point in the history
  • Loading branch information
tbombach committed Apr 14, 2016
1 parent 166ff89 commit 59d3fa9
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
1 change: 1 addition & 0 deletions AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,7 @@ public void UrlPathTests()
client.Paths.Base64Url(Encoding.UTF8.GetBytes("lorem"));
var testArray = new List<string> { "ArrayPath1", @"begin!*'();:@ &=+$,/?#[]end", null, "" };
client.Paths.ArrayCsvInPath(testArray);
client.Paths.UnixTimeUrl(new DateTime(2016, 4, 13, 0, 0, 0));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,18 @@ public partial interface IPaths
/// The cancellation token.
/// </param>
Task<HttpOperationResponse> ArrayCsvInPathWithHttpMessagesAsync(IList<string> arrayPath, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Get the date 2016-04-13 encoded value as '1460505600' (Unix time)
/// </summary>
/// <param name='unixTimeUrlPath'>
/// Unix time encoded value
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse> UnixTimeUrlWithHttpMessagesAsync(DateTime unixTimeUrlPath, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2692,5 +2692,111 @@ public Paths(AutoRestUrlTestService client)
return _result;
}

/// <summary>
/// Get the date 2016-04-13 encoded value as '1460505600' (Unix time)
/// </summary>
/// <param name='unixTimeUrlPath'>
/// Unix time encoded value
/// </param>
/// <param name='customHeaders'>
/// Headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
/// <return>
/// A response object containing the response body and response headers.
/// </return>
public async Task<HttpOperationResponse> UnixTimeUrlWithHttpMessagesAsync(DateTime unixTimeUrlPath, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
{
// Tracing
bool _shouldTrace = ServiceClientTracing.IsEnabled;
string _invocationId = null;
if (_shouldTrace)
{
_invocationId = ServiceClientTracing.NextInvocationId.ToString();
Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
tracingParameters.Add("unixTimeUrlPath", unixTimeUrlPath);
tracingParameters.Add("cancellationToken", cancellationToken);
ServiceClientTracing.Enter(_invocationId, this, "UnixTimeUrl", tracingParameters);
}
// Construct URL
var _baseUrl = this.Client.BaseUri.AbsoluteUri;
var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "paths/int/1460505600/{unixTimeUrlPath}").ToString();
_url = _url.Replace("{unixTimeUrlPath}", Uri.EscapeDataString(SafeJsonConvert.SerializeObject(unixTimeUrlPath, new UnixTimeJsonConverter()).Trim('"')));
// Create HTTP transport objects
HttpRequestMessage _httpRequest = new HttpRequestMessage();
HttpResponseMessage _httpResponse = null;
_httpRequest.Method = new HttpMethod("GET");
_httpRequest.RequestUri = new Uri(_url);
// Set Headers
if (customHeaders != null)
{
foreach(var _header in customHeaders)
{
if (_httpRequest.Headers.Contains(_header.Key))
{
_httpRequest.Headers.Remove(_header.Key);
}
_httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
}
}

// Serialize Request
string _requestContent = null;
// Send Request
if (_shouldTrace)
{
ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
}
cancellationToken.ThrowIfCancellationRequested();
_httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
if (_shouldTrace)
{
ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
}
HttpStatusCode _statusCode = _httpResponse.StatusCode;
cancellationToken.ThrowIfCancellationRequested();
string _responseContent = null;
if ((int)_statusCode != 200)
{
var ex = new ErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
try
{
_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
Error _errorBody = SafeJsonConvert.DeserializeObject<Error>(_responseContent, this.Client.DeserializationSettings);
if (_errorBody != null)
{
ex.Body = _errorBody;
}
}
catch (JsonException)
{
// Ignore the exception
}
ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
if (_shouldTrace)
{
ServiceClientTracing.Error(_invocationId, ex);
}
_httpRequest.Dispose();
if (_httpResponse != null)
{
_httpResponse.Dispose();
}
throw ex;
}
// Create Result
var _result = new HttpOperationResponse();
_result.Request = _httpRequest;
_result.Response = _httpResponse;
if (_shouldTrace)
{
ServiceClientTracing.Exit(_invocationId, _result);
}
return _result;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -712,5 +712,36 @@ public static void ArrayCsvInPath(this IPaths operations, IList<string> arrayPat
await operations.ArrayCsvInPathWithHttpMessagesAsync(arrayPath, null, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Get the date 2016-04-13 encoded value as '1460505600' (Unix time)
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
/// <param name='unixTimeUrlPath'>
/// Unix time encoded value
/// </param>
public static void UnixTimeUrl(this IPaths operations, DateTime unixTimeUrlPath)
{
Task.Factory.StartNew(s => ((IPaths)s).UnixTimeUrlAsync(unixTimeUrlPath), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
}

/// <summary>
/// Get the date 2016-04-13 encoded value as '1460505600' (Unix time)
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
/// <param name='unixTimeUrlPath'>
/// Unix time encoded value
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task UnixTimeUrlAsync(this IPaths operations, DateTime unixTimeUrlPath, CancellationToken cancellationToken = default(CancellationToken))
{
await operations.UnixTimeUrlWithHttpMessagesAsync(unixTimeUrlPath, null, cancellationToken).ConfigureAwait(false);
}

}
}
1 change: 1 addition & 0 deletions AutoRest/TestServer/server/routes/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var scenarioMap = {
"2012-01-01T01:01:01Z": "Valid",
"green color" : "Valid",
"bG9yZW0" : "Base64Url",
"1460505600": "UnixTime",
"ArrayPath1,begin!*'();:@ &=+$,/?#[]end,,": "CSVInPath"
};

Expand Down
30 changes: 30 additions & 0 deletions AutoRest/TestServer/swagger/url.json
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,36 @@
}
}
},
"/paths/int/1460505600/{unixTimeUrlPath}": {
"get": {
"operationId": "paths_unixTimeUrl",
"description": "Get the date 2016-04-13 encoded value as '1460505600' (Unix time)",
"tags": [
"Path Operations"
],
"parameters": [
{
"name": "unixTimeUrlPath",
"in": "path",
"description": "Unix time encoded value",
"type": "integer",
"format": "unixtime",
"required": true
}
],
"responses": {
"200": {
"description": "Successfully Received date 2016-04-13 encoded value as '1460505600' (Unix time)"
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "#/definitions/Error"
}
}
}
}
},
"/queries/bool/true": {
"get": {
"operationId": "queries_getBooleanTrue",
Expand Down

0 comments on commit 59d3fa9

Please sign in to comment.