private static bool ValidateResponseHeader(ZumoTest test, HttpStatusCode expectedStatus, Dictionary <string, string> expectedHeaders, HttpResponseMessage response) { bool result = true; if (expectedStatus != response.StatusCode) { test.AddLog("Error in response status: expected {0}, received {1}", expectedStatus, response.StatusCode); result = false; } else { foreach (var reqHeader in expectedHeaders.Keys) { IEnumerable <string> headerValue; if (!response.Headers.TryGetValues(reqHeader, out headerValue)) { test.AddLog("Error, expected header {0} not found", reqHeader); result = false; break; } else { if (!expectedHeaders[reqHeader].Equals(headerValue.FirstOrDefault())) { test.AddLog("Error, header value for {0} is incorrect. Expected {1}, actual {2}", reqHeader, expectedHeaders[reqHeader], headerValue.FirstOrDefault() ?? "<<NULL>>"); result = false; break; } } } } return(result); }
public async Task <IServiceFilterResponse> HandleRequest(IServiceFilterRequest request, IServiceFilterContinuation continuation) { IServiceFilterResponse response = null; try { for (int i = 0; i < this.NumberOfRequests; i++) { response = await continuation.Handle(request); test.AddLog("Sent the request number {0}", i + 1); test.AddLog("Response: {0} - {1}", response.StatusCode, response.StatusDescription); if (response.StatusCode >= 400) { test.AddLog("Invalid response. Content-Type: {0}", response.ContentType); test.AddLog("Response content: {0}", response.Content); throw new InvalidOperationException(); } } } catch (Exception ex) { test.AddLog("Exception while calling continuation: {0}", ex); this.TestFailed = true; throw; } return(response); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { HttpResponseMessage response = null; try { for (int i = 0; i < this.NumberOfRequests; i++) { HttpRequestMessage clonedRequest = await CloneRequest(request); response = await base.SendAsync(clonedRequest, cancellationToken); if (i < this.NumberOfRequests - 1) { response.Dispose(); response = null; } } } catch (Exception ex) { test.AddLog("Exception while calling continuation: {0}", ex); this.TestFailed = true; throw; } return(response); }
private static bool Validate404Response(ZumoTest test, MobileServiceInvalidOperationException msioe) { test.AddLog("Received expected exception - {0}: {1}", msioe.GetType().FullName, msioe.Message); var response = msioe.Response; if (response.StatusCode == 404) { test.AddLog("And error code is the expected one."); return(true); } else { test.AddLog("Received error code is not the expected one: {0} - {1}", response.StatusCode, response.StatusDescription); return(false); } }
public Task <JObject> ExecuteTableOperationAsync(IMobileServiceTableOperation operation) { var itemId = (string)operation.Item[MobileServiceSystemColumns.Id]; if (this.AbortCondition(itemId)) { test.AddLog("Found id to abort ({0}), aborting the push operation"); operation.AbortPush(); } else { test.AddLog("Pushing operation {0} for item {1}", operation.Kind, itemId); } return(operation.ExecuteAsync()); }
static async Task WaitForChannelUriAssignment(ZumoTest test, HttpNotificationChannel pushChannel, TimeSpan maxWait) { DateTime start = DateTime.UtcNow; while (DateTime.UtcNow.Subtract(start) < maxWait) { if (pushChannel.ConnectionStatus == ChannelConnectionStatus.Connected && pushChannel.ChannelUri != null) { test.AddLog("Channel URI: {0}", pushChannel.ChannelUri); break; } else { test.AddLog("Waiting for the push channel URI to be assigned"); } await Util.TaskDelay(500); } }
public async Task <JObject> ExecuteTableOperationAsync(IMobileServiceTableOperation operation) { MobileServicePreconditionFailedException ex = null; JObject result = null; do { ex = null; try { test.AddLog("Attempting to execute the operation"); result = await operation.ExecuteAsync(); } catch (MobileServicePreconditionFailedException e) { ex = e; } if (ex != null) { test.AddLog("A MobileServicePreconditionFailedException was thrown, ex.Value = {0}", ex.Value); var serverItem = ex.Value; if (serverItem == null) { test.AddLog("Item not returned in the exception, trying to retrieve it from the server"); serverItem = (JObject)(await client.GetTable(operation.Table.TableName).LookupAsync((string)operation.Item["id"])); } var typedClientItem = operation.Item.ToObject <T>(); var typedServerItem = serverItem.ToObject <T>(); var typedMergedItem = conflictResolution(typedClientItem, typedServerItem); var mergedItem = JObject.FromObject(typedMergedItem); mergedItem[MobileServiceSystemColumns.Version] = serverItem[MobileServiceSystemColumns.Version]; test.AddLog("Merged the items, will try to resubmit the operation"); operation.Item = mergedItem; } } while (ex != null); return(result); }
private static bool ValidateExpectedError(ZumoTest test, MobileServiceInvalidOperationException exception, bool operationShouldSucceed) { if (operationShouldSucceed) { if (exception != null) { test.AddLog("Operation should have succeeded, but it failed: {0}", exception); return(false); } else { return(true); } } else { if (exception == null) { test.AddLog("Operation should have failed, but it succeeded."); return(false); } else { if (exception.Response.StatusCode == HttpStatusCode.Unauthorized || exception.Response.StatusCode == HttpStatusCode.Forbidden) { test.AddLog("Expected exception thrown, with expected status code."); return(true); } else { test.AddLog("Expected exception was thrown, but with invalid status code: {0}", exception.Response.StatusCode); return(false); } } } }
private static bool ValidateParameters(ZumoTest test, string operation, JObject expected, JObject actual) { test.AddLog("Called {0}, now validating parameters", operation); List <string> errors = new List <string>(); if (!Util.CompareJson(expected, actual, errors)) { foreach (var error in errors) { test.AddLog(error); } test.AddLog("Parameters passing for the {0} operation failed", operation); test.AddLog("Expected: {0}", expected); test.AddLog("Actual: {0}", actual); return(false); } else { test.AddLog("Parameters passing for the {0} operation succeeded", operation); return(true); } }