Created
August 3, 2012 20:54
-
-
Save alfeg/3251434 to your computer and use it in GitHub Desktop.
Restsharp client extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using Fasterflect; | |
using RestSharp; | |
namespace xxx.xxx.Rest.Client | |
{ | |
public static partial class RestClientExtensions | |
{ | |
public static ClientResponse ExecuteItDynamic(this IRestClient client, IRestRequest request) | |
{ | |
var sw = new Stopwatch(); | |
sw.Start(); | |
var response = client.Execute<dynamic>(request); | |
sw.Stop(); | |
Console.WriteLine("\tRestClient.Executed: {0}: {1}, took: {2}ms", request.Method, response.ResponseUri, sw.ElapsedMilliseconds); | |
if (response.ContentType.Contains("application/json")) | |
{ | |
var content = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(response.Content); | |
response.Data = content; | |
} | |
return ClientResponse.From(response); | |
} | |
public static void AddUrlSegment(this IRestRequest request, string name, int value) | |
{ | |
request.AddUrlSegment(name, value.ToString()); | |
} | |
public static void AddUrlSegment(this IRestRequest request, object value) | |
{ | |
foreach (var property in value.GetType().Properties()) | |
{ | |
request.AddUrlSegment(property.Name, property.Get(value).ToString()); | |
} | |
} | |
/// <summary> | |
/// Add request body to request. | |
/// If body is string, | |
//// then we assume that body is already in JSON format | |
/// else | |
/// body will be serialized to JSON | |
/// </summary> | |
/// <param name="request"></param> | |
/// <param name="body"></param> | |
public static void AddRequestBody(this IRestRequest request, object body) | |
{ | |
if (body is string) | |
{ | |
request.AddParameter(new Parameter | |
{ | |
Name = "application/json", | |
Type = ParameterType.RequestBody, | |
Value = body | |
}); | |
} | |
else | |
request.AddBody(body); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//some Rest endpoints specs from api | |
public IRestRequest GetOrder(int restaurantId, int orderId) | |
{ | |
var request = NewRequest("restaurant/{restaurantId}/order/{orderId}", Method.GET); | |
request.AddUrlSegment(new {restaurantId, orderId}); | |
return request; | |
} | |
public IRestRequest GetMenu(int restaurantId, bool loadMenuItems = true) | |
{ | |
var request = NewRequest("restaurant/{restaurantId}/menu?loadMenuItems={loadMenuItems}"); | |
request.AddUrlSegment(new { restaurantId, loadMenuItems }); | |
return request; | |
} | |
private IRestRequest NewRequest(string uriTemplate, Method method = Method.GET) | |
{ | |
return this.Request = new RestRequest(uriTemplate, method) | |
{ | |
JsonSerializer = new JsonNetSerializer(), //own serializer for awful datetime conventions | |
RequestFormat = DataFormat.Json | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture(Category = "Integration", Ignore = true)] | |
public class When_I_am_guest_user_and_there_is_another_user_with_orders : Core.XXXRestApiSpecification | |
{ | |
private int anotherClientOrderId; | |
private DemoStore demoStore; | |
protected XXXClient AnotherClient { get; set; } | |
/// <summary> | |
/// Given guest account with started order | |
/// </summary> | |
public override void Given() | |
{ | |
base.Given(); | |
AnotherClient = GetNewXXXClient(); | |
demoStore = XXXTestConfiguration.DemoStore.Value; | |
} | |
public override void When() | |
{ | |
base.When(); | |
AnotherClient.Action.LoginAsGuest(); | |
var orderResult = AnotherClient.Action.StartOrder(demoStore.Id).AssertIsOk(); | |
anotherClientOrderId = orderResult.Data.order.orderId; | |
Client.Action.LoginAsGuest(); | |
} | |
[Test] | |
public void Then_I_cannot_getOrderInfo_of_another_user() | |
{ | |
Client.Action.GetOrderInfo(demoStore.Id, anotherClientOrderId).AssertIsNot(HttpStatusCode.OK); | |
} | |
[Test] | |
public void Then_I_cannot_delete_Order_of_another_user() | |
{ | |
Client.Action.DeleteOrder(demoStore.Id, anotherClientOrderId).AssertIsNot(HttpStatusCode.OK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment