Skip to content

Commit

Permalink
Merge pull request EasyHttp#21 from davidwalker/master
Browse files Browse the repository at this point in the history
Reuse CookieContainer between multiple requests
  • Loading branch information
hhariri committed Aug 18, 2013
2 parents 9273ae5 + cb5c44f commit eab6b66
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/EasyHttp.Specs/Helpers/ServiceStackHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class FilesResponse
public string Result { get; set; }
}

public class CookieInfo
{
public string Name { get; set; }
public string Value { get; set; }
}

public class HelloService : RestServiceBase<Hello>
{
public override object OnGet(Hello request)
Expand Down Expand Up @@ -63,6 +69,22 @@ public override object OnPost(Files request)
}
}

public class CookieService : RestServiceBase<CookieInfo>
{
public override object OnGet(CookieInfo request)
{
if (!Request.Cookies.ContainsKey(request.Name))
return new HttpResult {StatusCode = HttpStatusCode.NotFound};
return new CookieInfo() { Name = request.Name, Value = Request.Cookies[request.Name].Value };
}

public override object OnPut(CookieInfo request)
{
Response.Cookies.AddCookie(new Cookie(request.Name, request.Value));
return new HttpResult() { StatusCode = HttpStatusCode.OK };
}
}

//Define the Web Services AppHost
public class ServiceStackHost : AppHostHttpListenerBase
{
Expand All @@ -74,6 +96,7 @@ public override void Configure(Funq.Container container)
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name}");
Routes.Add<Files>("/fileupload/{Name}").Add<Files>("/fileupload");
Routes.Add<CookieInfo>("/cookie").Add<CookieInfo>("/cookie/{Name}");
}
}
}
55 changes: 55 additions & 0 deletions src/EasyHttp.Specs/Specs/HttpRequestSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,59 @@ public class when_making_a_PUT_request_with_valid_uri_and_valid_data_and_content
static dynamic response;
}

[Subject("HttpClient")]
public class when_making_requests_and_persisting_cookies
{
Establish context = () =>
{
httpClient = new HttpClient();
httpClient.Request.PersistCookies = true;
httpClient.Request.Accept = HttpContentTypes.ApplicationJson;
};

Because of = () =>
{
httpClient.Put("http://localhost:16000/cookie", new CookieInfo { Name = "test", Value = "test cookie" }, HttpContentTypes.ApplicationJson);
response = httpClient.Get("http://localhost:16000/cookie/test");
};


It should_send_returned_cookies = () =>
{
string cookieValue = response.DynamicBody.Value;

cookieValue.ShouldEqual("test cookie");
};

static HttpClient httpClient;
static dynamic response;
}

[Subject("HttpClient")]
public class when_making_requests_and_not_persisting_cookies
{
Establish context = () =>
{
httpClient = new HttpClient();
httpClient.Request.PersistCookies = false;
httpClient.Request.Accept = HttpContentTypes.ApplicationJson;
};

Because of = () =>
{
httpClient.Put("http://localhost:16000/cookie", new CookieInfo { Name = "test", Value = "test cookie" }, HttpContentTypes.ApplicationJson);
response = httpClient.Get("http://localhost:16000/cookie/test");
};


It should_not_send_returned_cookies = () =>
{
HttpStatusCode statusCode = response.StatusCode;

statusCode.ShouldEqual(HttpStatusCode.NotFound);
};

static HttpClient httpClient;
static dynamic response;
}
}
5 changes: 4 additions & 1 deletion src/EasyHttp/Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class HttpRequest
string _password;
string _username;
HttpWebRequest httpWebRequest;
CookieContainer cookieContainer;

public HttpRequest(IEncoder encoder)
{
Expand Down Expand Up @@ -108,6 +109,7 @@ public bool ForceBasicAuth
set { _forceBasicAuth = value; }
}

public bool PersistCookies { get; set; }


public void SetBasicAuthentication(string username, string password)
Expand All @@ -118,7 +120,8 @@ public void SetBasicAuthentication(string username, string password)

void SetupHeader()
{
var cookieContainer = new CookieContainer();
if(!PersistCookies || cookieContainer == null)
cookieContainer = new CookieContainer();

httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = ContentType;
Expand Down

0 comments on commit eab6b66

Please sign in to comment.