Skip to content

Commit

Permalink
Support of Basic authentification header for REST testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bfftjenkins committed Sep 21, 2011
1 parent bee6824 commit a352605
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
74 changes: 44 additions & 30 deletions MvcIntegrationTestFramework/Browsing/BrowsingSession.cs
@@ -1,4 +1,5 @@
using System;
using System.Text;
using System.Collections.Specialized;
using System.IO;
using System.Web;
Expand All @@ -25,13 +26,12 @@ public RequestResult Get(string url)

public RequestResult Get(string url, string acceptHeader)
{
NameValueCollection headers = new NameValueCollection();
return this.Get(url, acceptHeader, "", "");
}

if (!String.IsNullOrEmpty(acceptHeader))
{
headers.Add("Accept", acceptHeader);
}
return ProcessRequest(url, HttpVerbs.Get, new NameValueCollection(), headers);
public RequestResult Get(string url, string acceptHeader, string authorizeUsername, string authorizePassword)
{
return ProcessRequest(url, HttpVerbs.Get, new NameValueCollection(), acceptHeader, authorizeUsername, authorizePassword);
}

/// <summary>
Expand Down Expand Up @@ -61,57 +61,70 @@ public RequestResult Post(string url, object formData)

public RequestResult Post(string url, object formData, string acceptHeader)
{
NameValueCollection headers = new NameValueCollection();
return this.Post(url, formData, acceptHeader, "", "");
}

if (!String.IsNullOrEmpty(acceptHeader))
{
headers.Add("Accept", acceptHeader);
}
public RequestResult Post(string url, object formData, string acceptHeader, string authorizeUsername, string authorizePassword)
{
return ProcessRequest(url, HttpVerbs.Post, formData, acceptHeader, authorizeUsername, authorizePassword);
}

var formNameValueCollection = NameValueCollectionConversions.ConvertFromObject(formData);
return ProcessRequest(url, HttpVerbs.Post, formNameValueCollection, headers);
public RequestResult Delete(string url)
{
return this.Delete(url, "");
}

public RequestResult Delete(string url, object formData)
public RequestResult Delete(string url, string acceptHeader)
{
return this.Delete(url, formData, "");
return this.Delete(url, acceptHeader, "", "");
}

public RequestResult Delete(string url, object formData, string acceptHeader)
public RequestResult Delete(string url, string acceptHeader, string authorizeUsername, string authorizePassword)
{
return ProcessRequest(url, HttpVerbs.Delete, new NameValueCollection(), acceptHeader, authorizeUsername, authorizePassword);
}

private RequestResult ProcessRequest(string url, HttpVerbs httpVerb, object formData, string acceptHeader, string authorizeUsername, string authorizePassword)
{
NameValueCollection headers = new NameValueCollection();

NameValueCollection headers = new NameValueCollection();
if (!String.IsNullOrEmpty(acceptHeader))
{
headers.Add("Accept", acceptHeader);
// -> http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
}
if (!String.IsNullOrEmpty(authorizeUsername))
{
headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(authorizeUsername + ":" + authorizePassword)));
// -> http://en.wikipedia.org/wiki/Basic_access_authentication
}

var formNameValueCollection = NameValueCollectionConversions.ConvertFromObject(formData);
return ProcessRequest(url, HttpVerbs.Delete, formNameValueCollection, headers);
}
return ProcessRequest(url, httpVerb, formNameValueCollection, headers);

private RequestResult ProcessRequest(string url, HttpVerbs httpVerb = HttpVerbs.Get, NameValueCollection formValues = null)
{
return ProcessRequest(url, httpVerb, formValues, null);
}

private RequestResult ProcessRequest(string url, HttpVerbs httpVerb, NameValueCollection formValues, NameValueCollection headers)
{
if (url == null) throw new ArgumentNullException("url");
if (url == null)
{
throw new ArgumentNullException("url");
}

// Fix up URLs that incorrectly start with / or ~/
if (url.StartsWith("~/"))
url = url.Substring(2);
else if(url.StartsWith("/"))
else if (url.StartsWith("/"))
url = url.Substring(1);

// Parse out the querystring if provided
string query = "";
int querySeparatorIndex = url.IndexOf("?");
if (querySeparatorIndex >= 0) {
if (querySeparatorIndex >= 0)
{
query = url.Substring(querySeparatorIndex + 1);
url = url.Substring(0, querySeparatorIndex);
}
}

// Perform the request
LastRequestData.Reset();
Expand All @@ -134,18 +147,19 @@ private RequestResult ProcessRequest(string url, HttpVerbs httpVerb, NameValueCo

private void AddAnyNewCookiesToCookieCollection()
{
if(LastRequestData.Response == null)
if (LastRequestData.Response == null)
return;

HttpCookieCollection lastResponseCookies = LastRequestData.Response.Cookies;
if(lastResponseCookies == null)
if (lastResponseCookies == null)
return;

foreach (string cookieName in lastResponseCookies) {
foreach (string cookieName in lastResponseCookies)
{
HttpCookie cookie = lastResponseCookies[cookieName];
if (Cookies[cookieName] != null)
Cookies.Remove(cookieName);
if((cookie.Expires == default(DateTime)) || (cookie.Expires > DateTime.Now))
if ((cookie.Expires == default(DateTime)) || (cookie.Expires > DateTime.Now))
Cookies.Add(cookie);
}
}
Expand Down
1 change: 1 addition & 0 deletions MvcIntegrationTestFramework/Hosting/AppHost.cs
Expand Up @@ -41,6 +41,7 @@ public void Start(Action<BrowsingSession> testScript)
_appDomainProxy.RunBrowsingSessionInAppDomain(serializableDelegate);
}


#region Initializing app & interceptors
private static void InitializeApplication()
{
Expand Down

0 comments on commit a352605

Please sign in to comment.