Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(622)

Side by Side Diff: Src/GoogleApis.Tests/Apis/Requests/ClientServiceRequestTest.cs

Issue 12772043: Issues 373 (Execute Bug), 374 (Remove Tests.Utility) and 375 (Clean warnings) (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Patch Set: Error in uploading the first time Created 10 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 Copyright 2011 Google Inc 2 Copyright 2011 Google Inc
3 3
4 Licensed under the Apache License, Version 2.0 (the "License"); 4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License. 5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at 6 You may obtain a copy of the License at
7 7
8 http://www.apache.org/licenses/LICENSE-2.0 8 http://www.apache.org/licenses/LICENSE-2.0
9 9
10 Unless required by applicable law or agreed to in writing, software 10 Unless required by applicable law or agreed to in writing, software
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 public string Name { get; set; } 67 public string Name { get; set; }
68 68
69 [Newtonsoft.Json.JsonPropertyAttribute("id")] 69 [Newtonsoft.Json.JsonPropertyAttribute("id")]
70 public int Id { get; set; } 70 public int Id { get; set; }
71 71
72 public override bool Equals(object obj) 72 public override bool Equals(object obj)
73 { 73 {
74 var other = obj as MockResponse; 74 var other = obj as MockResponse;
75 return (other != null && other.ETag == ETag && other.Name == Nam e && other.Id == Id); 75 return (other != null && other.ETag == ETag && other.Name == Nam e && other.Id == Id);
76 } 76 }
77
78 public override int GetHashCode()
79 {
80 return (ETag ?? string.Empty).GetHashCode() ^ (Name ?? string.Em pty).GetHashCode() Id;
81 }
77 } 82 }
78 83
79 /// <summary> A mock request class. </summary> 84 /// <summary> A mock request class. </summary>
80 class MockRequest : IDirectResponseSchema 85 class MockRequest : IDirectResponseSchema
81 { 86 {
82 [Newtonsoft.Json.JsonPropertyAttribute("etag")] 87 [Newtonsoft.Json.JsonPropertyAttribute("etag")]
83 public string ETag { get; set; } 88 public string ETag { get; set; }
84 89
85 [Newtonsoft.Json.JsonPropertyAttribute("long_name")] 90 [Newtonsoft.Json.JsonPropertyAttribute("long_name")]
86 public string Name { get; set; } 91 public string Name { get; set; }
87 92
88 public override bool Equals(object obj) 93 public override bool Equals(object obj)
89 { 94 {
90 var other = obj as MockRequest; 95 var other = obj as MockRequest;
91 return (other != null && other.ETag == ETag && other.Name == Nam e); 96 return (other != null && other.ETag == ETag && other.Name == Nam e);
92 } 97 }
98
99 public override int GetHashCode()
100 {
101 return (ETag ?? string.Empty).GetHashCode() ^ (Name ?? string.Em pty).GetHashCode();
102 }
93 } 103 }
94 104
95 /// <summary> A mock service request which returns <see cref="MockRespon se"/>. </summary> 105 /// <summary> A mock service request which returns <see cref="MockRespon se"/>. </summary>
96 class TestClientServiceRequest : ClientServiceRequest<MockResponse> 106 class TestClientServiceRequest : ClientServiceRequest<MockResponse>
97 { 107 {
98 /// <summary> Gets or sets a request number. It's used on concurrent tests. </summary> 108 /// <summary> Gets or sets a request number. It's used on concurrent tests. </summary>
99 public int CallNum { get; set; } 109 public int CallNum { get; set; }
100 private string httpMethod; 110 private string httpMethod;
101 private object body; 111 private object body;
102 112
(...skipping 25 matching lines...) Expand all
128 return body; 138 return body;
129 } 139 }
130 } 140 }
131 141
132 /// <summary> A mock message handler which returns an error. </summary> 142 /// <summary> A mock message handler which returns an error. </summary>
133 class ErrorMessageHanlder : CountableMessageHandler 143 class ErrorMessageHanlder : CountableMessageHandler
134 { 144 {
135 public string ExpectedError = 145 public string ExpectedError =
136 @"Message[Login Required] Location[Authorization - header] Reaso n[required] Domain[global]"; 146 @"Message[Login Required] Location[Authorization - header] Reaso n[required] Domain[global]";
137 147
138 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt pRequestMessage request, 148 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque stMessage request,
139 CancellationToken cancellationToken) 149 CancellationToken cancellationToken)
140 { 150 {
141 var response = new HttpResponseMessage();
142 var error = @"{ 151 var error = @"{
143 ""error"": { 152 ""error"": {
144 ""errors"": [ 153 ""errors"": [
145 { 154 {
146 ""domain"": ""global"", 155 ""domain"": ""global"",
147 ""reason"": ""required"", 156 ""reason"": ""required"",
148 ""message"": ""Login Required"", 157 ""message"": ""Login Required"",
149 ""locationType"": ""header"", 158 ""locationType"": ""header"",
150 ""location"": ""Authorization"" 159 ""location"": ""Authorization""
151 } 160 }
152 ], 161 ],
153 ""code"": 401, 162 ""code"": 401,
154 ""message"": ""Login Required"" 163 ""message"": ""Login Required""
155 } 164 }
156 }"; 165 }";
157 response.Content = new StringContent(error); 166
158 response.StatusCode = System.Net.HttpStatusCode.Unauthorized; 167 var response = new HttpResponseMessage
159 return response; 168 {
169 Content = new StringContent(error),
170 StatusCode = System.Net.HttpStatusCode.Unauthorized
171 };
172
173 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti onSource<HttpResponseMessage>();
174 tcs.SetResult(response);
175 return tcs.Task;
160 } 176 }
161 } 177 }
162 178
163 /// <summary> Tests message handler which tests the content on the reque st and the response. </summary> 179 /// <summary> Tests message handler which tests the content on the reque st and the response. </summary>
164 class TestBodyMessageHnalder : CountableMessageHandler 180 class TestBodyMessageHnalder : CountableMessageHandler
165 { 181 {
166 /// <summary> Gets or sets indication is GZip is eanbled. </summary> 182 /// <summary> Gets or sets indication is GZip is eanbled. </summary>
167 public bool GZipEnabled { get; set; } 183 public bool GZipEnabled { get; set; }
168 184
169 /// <summary> Gets or sets the expected request object. </summary> 185 /// <summary> Gets or sets the expected request object. </summary>
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 253
238 /// <summary> A message handler which returns an Http response message o r throw an exception. </summary> 254 /// <summary> A message handler which returns an Http response message o r throw an exception. </summary>
239 class MockMessageHandler : CountableMessageHandler 255 class MockMessageHandler : CountableMessageHandler
240 { 256 {
241 private bool ThrowException { get; set; } 257 private bool ThrowException { get; set; }
242 public MockMessageHandler(bool throwException = false) 258 public MockMessageHandler(bool throwException = false)
243 { 259 {
244 ThrowException = throwException; 260 ThrowException = throwException;
245 } 261 }
246 262
247 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt pRequestMessage request, 263 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque stMessage request,
248 CancellationToken cancellationToken) 264 CancellationToken cancellationToken)
249 { 265 {
250 if (ThrowException) 266 if (ThrowException)
251 { 267 {
252 throw new InvalidOperationMockException("INVALID"); 268 throw new InvalidOperationMockException("INVALID");
253 } 269 }
254 return new HttpResponseMessage(); 270
271 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti onSource<HttpResponseMessage>();
272 tcs.SetResult(new HttpResponseMessage());
273 return tcs.Task;
255 } 274 }
256 } 275 }
257 276
258 /// <summary> A message handler which is used to cancel an Http request in the middle.</summary> 277 /// <summary> A message handler which is used to cancel an Http request in the middle.</summary>
259 class CancelRedirectMessageHandler : CountableMessageHandler 278 class CancelRedirectMessageHandler : CountableMessageHandler
260 { 279 {
261 /// <summary> The cancellation token we are going to use to cancel a request.</summary> 280 /// <summary> The cancellation token we are going to use to cancel a request.</summary>
262 public CancellationTokenSource CancellationTokenSource { get; set; } 281 public CancellationTokenSource CancellationTokenSource { get; set; }
263 282
264 /// <summary> The request index we are going to cancel.</summary> 283 /// <summary> The request index we are going to cancel.</summary>
265 public int CancelRequestNum { get; set; } 284 public int CancelRequestNum { get; set; }
266 285
267 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt pRequestMessage request, 286 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque stMessage request,
268 CancellationToken cancellationToken) 287 CancellationToken cancellationToken)
269 { 288 {
270 if (Calls == CancelRequestNum) 289 if (Calls == CancelRequestNum)
271 { 290 {
272 CancellationTokenSource.Cancel(); 291 CancellationTokenSource.Cancel();
273 } 292 }
274 var response = new HttpResponseMessage() 293 var response = new HttpResponseMessage()
275 { 294 {
276 StatusCode = HttpStatusCode.Redirect, 295 StatusCode = HttpStatusCode.Redirect,
277 RequestMessage = request 296 RequestMessage = request
278 }; 297 };
279 response.Headers.Location = new Uri("http://www.test.com"); 298 response.Headers.Location = new Uri("http://www.test.com");
280 return response; 299
300 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti onSource<HttpResponseMessage>();
301 tcs.SetResult(response);
302 return tcs.Task;
281 } 303 }
282 } 304 }
283 305
284 /// <summary>· 306 /// <summary>·
285 /// A message handler which checks concurrent calls (each odd request wi ll succeeded, and even request will· 307 /// A message handler which checks concurrent calls (each odd request wi ll succeeded, and even request will·
286 /// fail on the first try and will succeeded in the second try. 308 /// fail on the first try and will succeeded in the second try.
287 /// </summary> 309 /// </summary>
288 class ConcurrentCallsHandler : CountableMessageHandler 310 class ConcurrentCallsHandler : CountableMessageHandler
289 { 311 {
290 /// <summary> Gets or sets the Serializer which is used to serialize and deserialize messages. </summary> 312 /// <summary> Gets or sets the Serializer which is used to serialize and deserialize messages. </summary>
291 public ISerializer Serializer { get; set; } 313 public ISerializer Serializer { get; set; }
292 314
293 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt pRequestMessage request, 315 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque stMessage request,
294 CancellationToken cancellationToken) 316 CancellationToken cancellationToken)
295 { 317 {
296 var response = new HttpResponseMessage(); 318 var response = new HttpResponseMessage();
297 var uri = request.RequestUri.AbsoluteUri; 319 var uri = request.RequestUri.AbsoluteUri;
298 int lastDigit = 0; 320 int lastDigit = 0;
299 if (int.TryParse(uri[uri.Length - 1].ToString(), out lastDigit) && lastDigit % 2 == 0) 321 if (int.TryParse(uri[uri.Length - 1].ToString(), out lastDigit) && lastDigit % 2 == 0)
300 { 322 {
301 response.StatusCode = System.Net.HttpStatusCode.ServiceUnava ilable; 323 response.StatusCode = System.Net.HttpStatusCode.ServiceUnava ilable;
302 request.RequestUri = new Uri(uri (lastDigit 1)); 324 request.RequestUri = new Uri(uri (lastDigit 1));
303 } 325 }
304 else 326 else
305 { 327 {
306 var mockObject = new MockResponse { Name = "Name-" lastDig it }; 328 var mockObject = new MockResponse { Name = "Name-" lastDig it };
307 var serializedObject = Serializer.Serialize(mockObject); 329 var serializedObject = Serializer.Serialize(mockObject);
308 response.Content = new StringContent(serializedObject, Encod ing.UTF8, "application/json"); 330 response.Content = new StringContent(serializedObject, Encod ing.UTF8, "application/json");
309 } 331 }
310 return response; 332
333 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti onSource<HttpResponseMessage>();
334 tcs.SetResult(response);
335 return tcs.Task;
311 } 336 }
312 337
313 /// <summary> Unsuccessful response handler which "handles" service unavailable responses. </summary> 338 /// <summary> Unsuccessful response handler which "handles" service unavailable responses. </summary>
314 internal class ServiceUnavailableUnsuccessfulResponseHandler : IHttp UnsuccessfulResponseHandler 339 internal class ServiceUnavailableUnsuccessfulResponseHandler : IHttp UnsuccessfulResponseHandler
315 { 340 {
316 public bool HandleResponse(HandleUnsuccessfulResponseArgs args) 341 public bool HandleResponse(HandleUnsuccessfulResponseArgs args)
317 { 342 {
318 return args.Response.StatusCode == System.Net.HttpStatusCode .ServiceUnavailable; 343 return args.Response.StatusCode == System.Net.HttpStatusCode .ServiceUnavailable;
319 } 344 }
320 } 345 }
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
807 832
808 [RequestParameterAttribute("optionalEmpty", Google.Apis.Util.Request ParameterType.Query)] 833 [RequestParameterAttribute("optionalEmpty", Google.Apis.Util.Request ParameterType.Query)]
809 public string OptionalEmpty { get; set; } 834 public string OptionalEmpty { get; set; }
810 835
811 [RequestParameterAttribute("optionalNotPressent", Google.Apis.Util.R equestParameterType.Query)] 836 [RequestParameterAttribute("optionalNotPressent", Google.Apis.Util.R equestParameterType.Query)]
812 public string OptionalNotPressent { get; set; } 837 public string OptionalNotPressent { get; set; }
813 838
814 public ClientServiceRequestWithQueryParameters(IClientService servic e, string method, object body) 839 public ClientServiceRequestWithQueryParameters(IClientService servic e, string method, object body)
815 : base(service, method, body) 840 : base(service, method, body)
816 { 841 {
817 RequestParameters.Add("required", new MockParameter 842 RequestParameters.Add("required", new Parameter
818 { 843 {
819 Name = "required", 844 Name = "required",
820 IsRequired = true, 845 IsRequired = true,
821 ParameterType = "query" 846 ParameterType = "query"
822 }); 847 });
823 RequestParameters.Add("optionalWithValue", new MockParameter 848 RequestParameters.Add("optionalWithValue", new Parameter
824 { 849 {
825 Name = "optionalWithValue", 850 Name = "optionalWithValue",
826 IsRequired = false, 851 IsRequired = false,
827 ParameterType = "query", 852 ParameterType = "query",
828 DefaultValue = "DoesNotDisplay" 853 DefaultValue = "DoesNotDisplay"
829 }); 854 });
830 RequestParameters.Add("optionalWithValue2", new MockParameter 855 RequestParameters.Add("optionalWithValue2", new Parameter
831 { 856 {
832 Name = "optionalWithValue", 857 Name = "optionalWithValue",
833 IsRequired = false, 858 IsRequired = false,
834 ParameterType = "query", 859 ParameterType = "query",
835 DefaultValue = "DoesNotDisplay" 860 DefaultValue = "DoesNotDisplay"
836 }); 861 });
837 RequestParameters.Add("optionalWithNull", new MockParameter 862 RequestParameters.Add("optionalWithNull", new Parameter
838 { 863 {
839 Name = "optionalWithNull", 864 Name = "optionalWithNull",
840 IsRequired = false, 865 IsRequired = false,
841 ParameterType = "query", 866 ParameterType = "query",
842 DefaultValue = "c" 867 DefaultValue = "c"
843 }); 868 });
844 RequestParameters.Add("optionalEmpty", new MockParameter 869 RequestParameters.Add("optionalEmpty", new Parameter
845 { 870 {
846 Name = "optionalEmpty", 871 Name = "optionalEmpty",
847 IsRequired = false, 872 IsRequired = false,
848 ParameterType = "query", 873 ParameterType = "query",
849 DefaultValue = "d" 874 DefaultValue = "d"
850 }); 875 });
851 RequestParameters.Add("optionalNotPressent", new MockParameter 876 RequestParameters.Add("optionalNotPressent", new Parameter
852 { 877 {
853 Name = "optionalNotPressent", 878 Name = "optionalNotPressent",
854 IsRequired = false, 879 IsRequired = false,
855 ParameterType = "query", 880 ParameterType = "query",
856 DefaultValue = "DoesNotDisplay" 881 DefaultValue = "DoesNotDisplay"
857 }); 882 });
858 } 883 }
859 } 884 }
860 885
861 /// <summary> Tests build request with query parameters. </summary> 886 /// <summary> Tests build request with query parameters. </summary>
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
908 public int TestParameterA { get; set; } 933 public int TestParameterA { get; set; }
909 934
910 [RequestParameter("path2", RequestParameterType.Path)] 935 [RequestParameter("path2", RequestParameterType.Path)]
911 public int TestParameterB { get; set; } 936 public int TestParameterB { get; set; }
912 937
913 public int TestParameterC { get; set; } 938 public int TestParameterC { get; set; }
914 939
915 public ClientServiceRequestWithPathParameters(IClientService service , string method, object body) 940 public ClientServiceRequestWithPathParameters(IClientService service , string method, object body)
916 : base(service, method, body) 941 : base(service, method, body)
917 { 942 {
918 RequestParameters.Add("path1", new MockParameter 943 RequestParameters.Add("path1", new Parameter
919 { 944 {
920 Name = "path1", 945 Name = "path1",
921 ParameterType = "path" 946 ParameterType = "path"
922 }); 947 });
923 RequestParameters.Add("path2", new MockParameter 948 RequestParameters.Add("path2", new Parameter
924 { 949 {
925 Name = "path2", 950 Name = "path2",
926 ParameterType = "path", 951 ParameterType = "path",
927 }); 952 });
928 } 953 }
929 954
930 public override string RestPath 955 public override string RestPath
931 { 956 {
932 get { return "restPath/{path1}/something/{path2}"; } 957 get { return "restPath/{path1}/something/{path2}"; }
933 } 958 }
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Post)); 1117 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Post));
1093 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Patch)); 1118 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Patch));
1094 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Put)); 1119 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Put));
1095 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Delete)); 1120 Assert.AreEqual(ETagAction.IfMatch, ClientServiceRequest<object>.Get DefaultETagAction(HttpConsts.Delete));
1096 Assert.AreEqual(ETagAction.Ignore, ClientServiceRequest<object>.GetD efaultETagAction("INVALID")); 1121 Assert.AreEqual(ETagAction.Ignore, ClientServiceRequest<object>.GetD efaultETagAction("INVALID"));
1097 } 1122 }
1098 1123
1099 #endregion 1124 #endregion
1100 } 1125 }
1101 } 1126 }
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b