Permalink
Browse files

Finished up with adding support for targeting by IP address and host.

  • Loading branch information...
1 parent 188f127 commit 1af311dd8b48a80f69ce9ec362dfba03e4feecf6 @lukebakken lukebakken committed May 17, 2012
Binary file not shown.
Binary file not shown.
@@ -14,6 +14,11 @@ public VcapClientResult Info()
return new VcapClientResult(true, r.Execute<Info>());
}
+ internal VcapRequest BuildInfoRequest()
+ {
+ return BuildVcapRequest(Constants.INFO_PATH);
+ }
+
public VcapClientResult Target(Uri argUri = null)
{
VcapClientResult rv;
@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.IO;
+ using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using IronFoundry.Properties;
@@ -46,6 +47,11 @@ public VcapClient(Cloud cloud)
this.cloud = cloud;
}
+ public VcapClient(Uri uri, IPAddress ipAddress)
+ {
+ credMgr = new VcapCredentialManager(uri, ipAddress);
+ }
+
public void ProxyAs(VcapUser user)
{
proxyUser = user;
@@ -67,6 +73,12 @@ public VcapClientResult Info()
return helper.Info();
}
+ internal VcapRequest GetRequestForTesting()
+ {
+ var helper = new MiscHelper(proxyUser, credMgr);
+ return helper.BuildInfoRequest();
+ }
+
public VcapClientResult Target(string uri)
{
var helper = new MiscHelper(proxyUser, credMgr);
@@ -82,6 +82,11 @@ public Uri CurrentTarget
get { return currentTarget ?? Constants.DEFAULT_LOCAL_TARGET; }
}
+ public IPAddress CurrentTargetIP
+ {
+ get { return currentTargetIP; }
+ }
+
public string CurrentToken
{
get
@@ -96,22 +101,31 @@ public string CurrentToken
}
}
+ public void SetTarget(string uri)
+ {
+ SetTarget(new Uri(uri));
+ }
+
public void SetTarget(Uri uri)
{
currentTarget = uri;
+ currentTargetIP = null;
}
public void SetTarget(Uri uri, IPAddress ip)
{
+ if (null == uri)
+ {
+ throw new ArgumentNullException("uri");
+ }
+ if (null == ip)
+ {
+ throw new ArgumentNullException("ip");
+ }
currentTarget = uri;
currentTargetIP = ip;
}
- public void SetTarget(string uri)
- {
- currentTarget = new Uri(uri);
- }
-
public void RegisterToken(string token)
{
var accessToken = new AccessToken(CurrentTarget, token);
@@ -208,7 +222,5 @@ private Uri ReadTargetFile()
return rv;
}
-
- public object CurrentTargetIP { get; set; }
}
}
@@ -25,10 +25,11 @@ public abstract class VcapRequestBase
(ushort)HttpStatusCode.HttpVersionNotSupported, // 505
};
- protected readonly VcapCredentialManager credentialManager;
- protected readonly RestClient client;
- protected readonly string proxyUserEmail;
+ private readonly VcapCredentialManager credentialManager;
+ private readonly RestClient client;
+ private readonly string proxyUserEmail;
+ private string requestHostHeader;
protected RestRequest request;
protected VcapRequestBase(string proxyUserEmail, VcapCredentialManager credentialManager)
@@ -86,6 +87,12 @@ private RestRequest BuildRestRequest(Method method)
Method = method,
JsonSerializer = serializer,
};
+
+ if (null != requestHostHeader)
+ {
+ rv.AddHeader("Host", requestHostHeader);
+ }
+
return rv;
}
@@ -96,10 +103,17 @@ private RestClient BuildClient()
private RestClient BuildClient(bool useAuth, Uri uri = null)
{
- string baseUrl = credentialManager.CurrentTarget.AbsoluteUri;
- if (null != uri)
+ Uri currentTargetUri = uri;
+ if (null == currentTargetUri)
{
- baseUrl = uri.AbsoluteUri;
+ currentTargetUri = credentialManager.CurrentTarget;
+ }
+
+ string baseUrl = currentTargetUri.AbsoluteUri;
+ if (null != credentialManager.CurrentTargetIP)
+ {
+ baseUrl = String.Format("{0}://{1}", Uri.UriSchemeHttp, credentialManager.CurrentTargetIP.ToString());
+ requestHostHeader = currentTargetUri.Host;
}
var deserializer = new NewtonsoftJsonDeserializer();
@@ -189,6 +203,21 @@ private static void ProcessResponse(IRestResponse response)
}
}
}
+
+ internal RestClient Client
+ {
+ get { return client; }
+ }
+
+ internal RestRequest Request
+ {
+ get { return request; }
+ }
+
+ internal string RequestHostHeader
+ {
+ get { return requestHostHeader; }
+ }
}
public class VcapRequest : VcapRequestBase
@@ -35,7 +35,6 @@ public void Can_Create_With_Host_And_IP()
{
string ipStr = "10.0.0.1";
Uri uri = new Uri("http://api.vcap-test.me");
-
IPAddress ip;
IPAddress.TryParse(ipStr, out ip);
@@ -50,5 +49,54 @@ public void Can_Create_With_Host_And_IP()
Assert.Equal(newTargetUri, credentialManager.CurrentTarget);
Assert.Null(credentialManager.CurrentTargetIP);
}
+
+ [Fact]
+ public void Host_And_IP_Both_Required()
+ {
+ string ipStr = "10.0.0.1";
+ Uri uri = new Uri("http://api.vcap-test.me");
+ IPAddress ip;
+ IPAddress.TryParse(ipStr, out ip);
+
+ VcapCredentialManager credentialManager = null;
+ try
+ {
+ credentialManager = new VcapCredentialManager((Uri)null, (IPAddress)null);
+ }
+ catch (Exception ex)
+ {
+ Assert.IsType<ArgumentNullException>(ex);
+ }
+
+ try
+ {
+ credentialManager = new VcapCredentialManager(uri, (IPAddress)null);
+ }
+ catch (Exception ex)
+ {
+ Assert.IsType<ArgumentNullException>(ex);
+ }
+
+ credentialManager = new VcapCredentialManager(uri);
+ Assert.Equal(uri, credentialManager.CurrentTarget);
+ Assert.Null(credentialManager.CurrentTargetIP);
+
+ try
+ {
+ credentialManager.SetTarget((Uri)null, (IPAddress)null);
+ }
+ catch (Exception ex)
+ {
+ Assert.IsType<ArgumentNullException>(ex);
+ }
+ try
+ {
+ credentialManager.SetTarget(uri, (IPAddress)null);
+ }
+ catch (Exception ex)
+ {
+ Assert.IsType<ArgumentNullException>(ex);
+ }
+ }
}
}
@@ -41,6 +41,9 @@
<AssemblyOriginatorKeyFile>IronFoundry.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="RestSharp, Version=103.1.0.0, Culture=neutral, PublicKeyToken=aed551a8db69a9bd, processorArchitecture=MSIL">
+ <HintPath>..\..\lib\RestSharp\RestSharp.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
@@ -59,6 +62,7 @@
<Compile Include="UserTests.cs" />
<Compile Include="UtilitiesTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="VcapClientTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\IronFoundry.Dea\IronFoundry.Dea.csproj">
@@ -0,0 +1,48 @@
+namespace IronFoundry.Test
+{
+ using System;
+ using System.Net;
+ using IronFoundry.Utilities;
+ using IronFoundry.Vcap;
+ using RestSharp;
+ using Xunit;
+
+ public class VcapClientTests
+ {
+ [Fact]
+ public void Test_Using_Host_And_IPAddress()
+ {
+ string ipStr = "10.0.0.1";
+ string host = "api.vcap.me";
+
+ var uri = new Uri("http://" + host);
+ IPAddress ip;
+ IPAddress.TryParse(ipStr, out ip);
+ var client = new VcapClient(uri, ip);
+ VcapRequest infoRequest = client.GetRequestForTesting();
+
+ RestClient restClient = infoRequest.Client;
+ RestRequest restRequest = infoRequest.Request;
+
+ Assert.Equal("http://" + ipStr, restClient.BaseUrl);
+ Assert.Equal(host, infoRequest.RequestHostHeader);
+
+ Assert.NotNull(restRequest.JsonSerializer);
+ Assert.IsType<NewtonsoftJsonSerializer>(restRequest.JsonSerializer);
+ }
+
+ [Fact(Skip="MANUAL")]
+ public void Test_Using_Host_And_IPAddress_Against_Local()
+ {
+ string ipStr = "172.21.114.11";
+ string host = "api.vcap.me";
+
+ var uri = new Uri("http://" + host);
+ IPAddress ip;
+ IPAddress.TryParse(ipStr, out ip);
+ var client = new VcapClient(uri, ip);
+ VcapClientResult rslt = client.Login("user@foo.com", "Password");
+ Assert.True(rslt.Success);
+ }
+ }
+}

0 comments on commit 1af311d

Please sign in to comment.