forked from pact-foundation/pact-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A few tweaks to the pact broker basic auth support
- Loading branch information
Neil Campbell
authored and
Neil Campbell
committed
Sep 21, 2015
1 parent
db67647
commit aa7fd5f
Showing
7 changed files
with
109 additions
and
113 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace PactNet.Tests | ||
{ | ||
public class PactUriOptionsTests | ||
{ | ||
[Fact] | ||
public void Ctor_WithEmptyUsername_ThrowsArgumentException() | ||
{ | ||
const string username = ""; | ||
const string password = "somepassword"; | ||
|
||
Assert.Throws<ArgumentException>(() => new PactUriOptions(username, password)); | ||
} | ||
|
||
[Fact] | ||
public void Ctor_WithInvalidUsername_ThrowsArgumentException() | ||
{ | ||
const string username = "some:user"; | ||
const string password = "somepassword"; | ||
|
||
Assert.Throws<ArgumentException>(() => new PactUriOptions(username, password)); | ||
} | ||
|
||
[Fact] | ||
public void Ctor_WithEmptyPassword_ThrowsArgumentException() | ||
{ | ||
const string username = "someuser"; | ||
const string password = ""; | ||
|
||
Assert.Throws<ArgumentException>(() => new PactUriOptions(username, password)); | ||
} | ||
|
||
[Fact] | ||
public void Ctor_WithValidUsernameAndPassword_ReturnsCorrectAuthorizationSchemeAndValue() | ||
{ | ||
const string username = "Aladdin"; | ||
const string password = "open sesame"; | ||
var expectedAuthScheme = "Basic"; | ||
var expectedAuthValue = "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; | ||
|
||
var options = new PactUriOptions(username, password); | ||
|
||
Assert.Equal(expectedAuthScheme, options.AuthorizationScheme); | ||
Assert.Equal(expectedAuthValue, options.AuthorizationValue); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -315,17 +315,12 @@ public void Verify_WithHttpPactFileUri_CallsHttpClientWithJsonGetRequest() | |
} | ||
|
||
[Fact] | ||
public void Verify_WithHttpsPactFileUri_AndBasicAuthUriOptions_CallsHttpClientWithJsonGetRequest() | ||
public void Verify_WithHttpsPactFileUri_CallsHttpClientWithJsonGetRequest() | ||
{ | ||
var serviceProvider = "Event API"; | ||
var serviceConsumer = "My client"; | ||
var pactUri = "https://yourpactserver.com/getlatestpactfile"; | ||
var pactFileJson = "{ \"provider\": { \"name\": \"Event API\" }, \"consumer\": { \"name\": \"My client\" }, \"interactions\": [{ \"description\": \"My Description\", \"provider_state\": \"My Provider State\" }, { \"description\": \"My Description 2\", \"provider_state\": \"My Provider State\" }, { \"description\": \"My Description\", \"provider_state\": \"My Provider State 2\" }], \"metadata\": { \"pactSpecificationVersion\": \"1.0.0\" } }"; | ||
var basicAuthUserName = "someuser"; | ||
var basicAuthPassword = "somepassword"; | ||
var expectedAuthHeader = Convert.ToBase64String( | ||
Encoding.UTF8.GetBytes( | ||
String.Format("{0}:{1}", basicAuthUserName, basicAuthPassword))); | ||
|
||
var pactVerifier = GetSubject(); | ||
|
||
|
@@ -337,44 +332,22 @@ public void Verify_WithHttpsPactFileUri_AndBasicAuthUriOptions_CallsHttpClientWi | |
pactVerifier | ||
.ServiceProvider(serviceProvider, new HttpClient()) | ||
.HonoursPactWith(serviceConsumer) | ||
.PactUri(pactUri, new PactUriOptions(basicAuthUserName, basicAuthPassword)); | ||
.PactUri(pactUri); | ||
|
||
pactVerifier.Verify(); | ||
Assert.Equal(_fakeHttpMessageHandler.RequestsRecieved.Single().Headers.Authorization.Parameter, | ||
expectedAuthHeader); | ||
|
||
Assert.Equal(HttpMethod.Get, _fakeHttpMessageHandler.RequestsRecieved.Single().Method); | ||
Assert.Equal("application/json", _fakeHttpMessageHandler.RequestsRecieved.Single().Headers.Single(x => x.Key == "Accept").Value.Single()); | ||
} | ||
|
||
[Fact] | ||
public void PactUri_WithBasicAuthUriOptions_InvalidPassword_ThrowsApplicationException() | ||
public void Verify_WithHttpsPactFileUriAndBasicAuthUriOptions_CallsHttpClientWithJsonGetRequestAndBasicAuthorizationHeader() | ||
{ | ||
var serviceProvider = "Event API"; | ||
var serviceConsumer = "My client"; | ||
var pactUri = "https://yourpactserver.com/getlatestpactfile"; | ||
var basicAuthUserName = "someuser"; | ||
var basicAuthPassword = ""; | ||
|
||
var pactVerifier = GetSubject(); | ||
|
||
Assert.Throws<ApplicationException>(() => pactVerifier | ||
.ServiceProvider(serviceProvider, new HttpClient()) | ||
.HonoursPactWith(serviceConsumer) | ||
.PactUri(pactUri, new PactUriOptions(basicAuthUserName, basicAuthPassword))); | ||
} | ||
|
||
[Fact] | ||
public void Verify_WithHttpsPactFileUri_AndBasicAuthUriOptions_InUrl_CallsHttpClientWithJsonGetRequest() | ||
{ | ||
var serviceProvider = "Event API"; | ||
var serviceConsumer = "My client"; | ||
var pactUri = "https://someuser:[email protected]/getlatestpactfile"; | ||
var pactFileJson = "{ \"provider\": { \"name\": \"Event API\" }, \"consumer\": { \"name\": \"My client\" }, \"interactions\": [{ \"description\": \"My Description\", \"provider_state\": \"My Provider State\" }, { \"description\": \"My Description 2\", \"provider_state\": \"My Provider State\" }, { \"description\": \"My Description\", \"provider_state\": \"My Provider State 2\" }], \"metadata\": { \"pactSpecificationVersion\": \"1.0.0\" } }"; | ||
var basicAuthUserName = "someuser"; | ||
var basicAuthPassword = "somepassword"; | ||
var expectedAuthHeader = Convert.ToBase64String( | ||
Encoding.UTF8.GetBytes( | ||
String.Format("{0}:{1}", basicAuthUserName, basicAuthPassword))); | ||
var options = new PactUriOptions("someuser", "somepassword"); | ||
|
||
var pactVerifier = GetSubject(); | ||
|
||
|
@@ -386,36 +359,14 @@ public void Verify_WithHttpsPactFileUri_AndBasicAuthUriOptions_InUrl_CallsHttpCl | |
pactVerifier | ||
.ServiceProvider(serviceProvider, new HttpClient()) | ||
.HonoursPactWith(serviceConsumer) | ||
.PactUri(pactUri); | ||
.PactUri(pactUri, options); | ||
|
||
pactVerifier.Verify(); | ||
Assert.Equal(_fakeHttpMessageHandler.RequestsRecieved.Single().Headers.Authorization.Parameter, | ||
expectedAuthHeader); | ||
|
||
Assert.Equal(HttpMethod.Get, _fakeHttpMessageHandler.RequestsRecieved.Single().Method); | ||
Assert.Equal("application/json", _fakeHttpMessageHandler.RequestsRecieved.Single().Headers.Single(x => x.Key == "Accept").Value.Single()); | ||
} | ||
|
||
[Fact] | ||
public void Verify_WithHttpsPactFileUri_AndBasicAuthUriOptions_InUrl_InvalidUserInfo_ThrowsException() | ||
{ | ||
var serviceProvider = "Event API"; | ||
var serviceConsumer = "My client"; | ||
var pactUri = "https://[email protected]/getlatestpactfile"; | ||
var pactFileJson = "{ \"provider\": { \"name\": \"Event API\" }, \"consumer\": { \"name\": \"My client\" }, \"interactions\": [{ \"description\": \"My Description\", \"provider_state\": \"My Provider State\" }, { \"description\": \"My Description 2\", \"provider_state\": \"My Provider State\" }, { \"description\": \"My Description\", \"provider_state\": \"My Provider State 2\" }], \"metadata\": { \"pactSpecificationVersion\": \"1.0.0\" } }"; | ||
|
||
var pactVerifier = GetSubject(); | ||
|
||
_fakeHttpMessageHandler.Response = new HttpResponseMessage(HttpStatusCode.OK) | ||
{ | ||
Content = new StringContent(pactFileJson, Encoding.UTF8, "application/json") | ||
}; | ||
|
||
pactVerifier | ||
.ServiceProvider(serviceProvider, new HttpClient()) | ||
.HonoursPactWith(serviceConsumer) | ||
.PactUri(pactUri); | ||
|
||
Assert.Throws<InvalidOperationException>(() => pactVerifier.Verify()); | ||
Assert.Equal(_fakeHttpMessageHandler.RequestsRecieved.Single().Headers.Authorization.Scheme, options.AuthorizationScheme); | ||
Assert.Equal(_fakeHttpMessageHandler.RequestsRecieved.Single().Headers.Authorization.Parameter, options.AuthorizationValue); | ||
} | ||
|
||
[Fact] | ||
|
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace PactNet | ||
{ | ||
public class PactUriOptions | ||
{ | ||
public PactUriOptions(string basicAuthUserName, string basicAuthPassword) | ||
private const string AuthScheme = "Basic"; | ||
private readonly string _username; | ||
private readonly string _password; | ||
|
||
internal string AuthorizationScheme | ||
{ | ||
if (String.IsNullOrEmpty(basicAuthUserName) || String.IsNullOrEmpty(basicAuthPassword)) | ||
throw new ApplicationException("Invalid username or password"); | ||
get { return AuthScheme; } | ||
} | ||
|
||
this.BasicAuthUserName = basicAuthUserName; | ||
this.BasicAuthPassword = basicAuthPassword; | ||
internal string AuthorizationValue | ||
{ | ||
get | ||
{ | ||
return Convert.ToBase64String( | ||
Encoding.UTF8.GetBytes( | ||
String.Format("{0}:{1}", _username, _password))); | ||
} | ||
} | ||
|
||
public string BasicAuthUserName { get; private set; } | ||
public string BasicAuthPassword { get; private set; } | ||
public PactUriOptions(string username, string password) | ||
{ | ||
if (String.IsNullOrEmpty(username)) | ||
{ | ||
throw new ArgumentException("username is null or empty."); | ||
} | ||
|
||
if (username.Contains(":")) | ||
{ | ||
throw new ArgumentException("username contains a ':' character, which is not allowed."); | ||
} | ||
|
||
if (String.IsNullOrEmpty(password)) | ||
{ | ||
throw new ArgumentException("password is null or empty."); | ||
} | ||
|
||
_username = username; | ||
_password = password; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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