Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/dnsimple-test/FixtureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using Castle.Core.Internal;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
Expand Down Expand Up @@ -65,9 +64,9 @@ private string LoadFixture()
return File.ReadAllText(path);
}

public List<Parameter> ExtractHeaders()
public List<HeaderParameter> ExtractHeaders()
{
var headers = new List<Parameter>();
var headers = new List<HeaderParameter>();

foreach (var line in GetLines())
{
Expand All @@ -76,7 +75,7 @@ public List<Parameter> ExtractHeaders()
if (line.Contains(':'))
{
var header = line.Split(':');
headers.Add(new Parameter(header[0], header[1], ParameterType.HttpHeader));
headers.Add(new HeaderParameter(header[0], header[1]));
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/dnsimple-test/MockDnsimpleClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using dnsimple;
using dnsimple.Services;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Extensions;
using ICredentials = dnsimple.ICredentials;

namespace dnsimple_test
Expand Down Expand Up @@ -82,7 +82,7 @@ public void SetUserAgent(string customUserAgent)

public string RequestSentTo()
{
return ((MockHttpService)Http).RequestUrlSent.UrlDecode();
return Uri.UnescapeDataString(((MockHttpService)Http).RequestUrlSent);
}

public Method HttpMethodUsed()
Expand Down Expand Up @@ -118,14 +118,16 @@ public override RequestBuilder RequestBuilder(string path)
return new RequestBuilder(path);
}

public override IRestResponse Execute(IRestRequest request)
public override RestResponse Execute(RestRequest request)
{
RequestUrlSent = new RestClient($"{_baseUrl}/v2/").BuildUri(request).ToString();
using var client = new RestClient(new Uri($"{_baseUrl}/v2/"));
RequestUrlSent = client.BuildUri(request).ToString();
MethodSent = request.Method;
try
{
PayloadSent = (string)request.Parameters.Find(x =>
x.ContentType.Equals("application/json")).Value;
var bodyParameter = request.Parameters
.FirstOrDefault(p => p.Type == ParameterType.RequestBody);
PayloadSent = bodyParameter?.Value?.ToString();
}
catch (Exception)
{
Expand Down Expand Up @@ -156,14 +158,16 @@ public override IRestResponse Execute(IRestRequest request)

public class MockResponse : RestResponse
{
public MockResponse(FixtureLoader loader)
public MockResponse(FixtureLoader loader) : base(new RestRequest())
{
StatusCode = loader.ExtractStatusCode();
Content = loader.ExtractJsonPayload();
Headers = loader.ExtractHeaders();
IsSuccessStatusCode = (int)StatusCode >= 200 && (int)StatusCode < 300;
ResponseStatus = ResponseStatus.Completed;
}

public void SetHeaders(List<Parameter> headers)
public void SetHeaders(List<HeaderParameter> headers)
{
Headers = headers;
}
Expand Down
8 changes: 4 additions & 4 deletions src/dnsimple-test/Services/CertificatesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public void PurchaseLetsEncryptCertificate(long accountId,
Assert.That(certificateOrdered.CreatedAt, Is.EqualTo(Convert.ToDateTime("2020-06-18T18:54:17Z")));
Assert.That(certificateOrdered.UpdatedAt, Is.EqualTo(Convert.ToDateTime("2020-06-18T18:54:17Z")));

Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand Down Expand Up @@ -375,7 +375,7 @@ public void IssueLetsEncryptCertificate(long accountId,
Assert.That(certificate.AlternateNames, Is.Empty);
Assert.That(certificate.AuthorityIdentifier, Is.EqualTo("letsencrypt"));

Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand Down Expand Up @@ -407,7 +407,7 @@ public void PurchaseLetsEncryptCertificateRenewal(long accountId,
Assert.That(renewalPurchased.CreatedAt, Is.EqualTo(Convert.ToDateTime("2020-06-18T19:56:20Z")));
Assert.That(renewalPurchased.UpdatedAt, Is.EqualTo(Convert.ToDateTime("2020-06-18T19:56:20Z")));

Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand Down Expand Up @@ -440,7 +440,7 @@ public void IssueLetsEncryptCertificateRenewal(long accountId,
Assert.That(renewalIssued.AlternateNames, Is.Empty);
Assert.That(renewalIssued.AuthorityIdentifier, Is.EqualTo("letsencrypt"));

Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/dnsimple-test/Services/ContactsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void CreateContact(long accountId, string expectedUrl)
Assert.That(created.Email, Is.EqualTo(contact.Email));

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
});
}

Expand Down Expand Up @@ -215,7 +215,7 @@ public void UpdateContact(long accountId, long contactId, string expectedUrl)
Assert.That(updated.AccountId, Is.EqualTo(accountId));

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PATCH));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Patch));
});
}

Expand All @@ -233,7 +233,7 @@ public void DeleteContact(long accountId, long contactId, string expectedUrl)
});

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete));
});
}

Expand Down
31 changes: 11 additions & 20 deletions src/dnsimple-test/Services/HttpTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using dnsimple;
using dnsimple.Services;
using dnsimple.Services.ListOptions;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
Expand All @@ -19,31 +18,23 @@ public class HttpTest
[Test]
public void ReturnsARequestBuilder()
{
var http = new HttpService(new RestClient(), new RequestBuilder());
var http = new HttpService(new RestClientWrapper(), new RequestBuilder());
Assert.That(http.RequestBuilder(""), Is.InstanceOf<RequestBuilder>());

}

[Test]
public void InvalidRequest()
{
var client = new Mock<IRestClient>();
var response = new Mock<IRestResponse>();
var request = new Mock<IRestRequest>();
var http = new HttpService(client.Object, new RequestBuilder());

response.SetupProperty(mock => mock.StatusCode,
HttpStatusCode.Unauthorized);
response.Setup(mock => mock.IsSuccessful).Returns(false);
response.Setup(mock => mock.Content)
.Returns("{\"message\": \"Authentication failed\"}");

client.Setup(mock => mock.Execute(request.Object))
.Returns(response.Object);
var response = new RestResponse(new RestRequest())
{
StatusCode = HttpStatusCode.Unauthorized,
Content = "{\"message\": \"Authentication failed\"}",
ResponseStatus = ResponseStatus.Completed,
};

Assert.Throws(Is.TypeOf<AuthenticationException>()
.And.Message.EqualTo("Authentication failed"),
delegate { http.Execute(request.Object); });
delegate { HttpService.HandleExceptions(response); });
}

[Test]
Expand Down Expand Up @@ -144,18 +135,18 @@ public void BuildsRequestWithPath()
[Test]
public void Resets()
{
_builder.Method(Method.HEAD);
_builder.Method(Method.Head);

Assert.That(_builder.Reset().Request, Is.Null);
}

[Test]
public void SetsTheMethod()
{
_builder.Method(Method.POST);
_builder.Method(Method.Post);
var request = _builder.Request;

Assert.That(request.Method, Is.EqualTo(Method.POST));
Assert.That(request.Method, Is.EqualTo(Method.Post));
}

[Test]
Expand Down
10 changes: 6 additions & 4 deletions src/dnsimple-test/Services/OAuth2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public class OAuth2Test
private static void SetupMockHttpService(Mock<HttpService> http,
IMock<RestResponse> mockRestResponse)
{
var response = new RestResponse();
response.Content = mockRestResponse.Object.Content;
var response = new RestResponse(new RestRequest())
{
Content = mockRestResponse.Object.Content,
};
http.Setup(mock =>
mock.RequestBuilder(It.IsAny<string>()))
.Returns(new RequestBuilder(""));
Expand All @@ -31,8 +33,8 @@ private static void SetupMockHttpService(Mock<HttpService> http,
[Test]
public void ExchangeAuthorizationForToken()
{
var http = new Mock<HttpService>(new RestClient(), new RequestBuilder());
var mockRestResponse = new Mock<RestResponse>();
var http = new Mock<HttpService>(new RestClientWrapper(), new RequestBuilder());
var mockRestResponse = new Mock<RestResponse>(new RestRequest());
var oauthService = new OAuth2Service(http.Object);

mockRestResponse.Object.Content =
Expand Down
4 changes: 2 additions & 2 deletions src/dnsimple-test/Services/RegistrarAutoRenewalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void EnableDomainAutoRenewal(long accountId, string domain, string expect
client.Registrar.EnableDomainAutoRenewal(accountId, domain);
});
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put));
});
}

Expand All @@ -45,7 +45,7 @@ public void DisableDomainAutoRenewal(long accountId, string domain, string expec
client.Registrar.DisableDomainAutoRenewal(accountId, domain);
});
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete));
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/dnsimple-test/Services/RegistrarDelegationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void ChangeDomainDelegation(long accountId, string domain,
Assert.That(newDelegation.Data, Is.EqualTo(delegation));

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put));
});
}

Expand Down Expand Up @@ -130,7 +130,7 @@ public void ChangeDomainDelegationToVanity(long accountId, string domain, string
Assert.That(vanityDelegation.First().UpdatedAt, Is.EqualTo(UpdatedAt));

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put));
});
}

Expand All @@ -150,7 +150,7 @@ public void ChangeDomainDelegationFromVanity(long accountId, string domain, stri
});

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete));
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/dnsimple-test/Services/RegistrarTransferLockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void EnableDomainTransferLock(long accountId, string domain, string expec
Assert.That(transferLockStatus.Enabled, Is.True);

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
});
}

Expand All @@ -46,7 +46,7 @@ public void DisableDomainTransferLock(long accountId, string domain, string expe
Assert.That(transferLockStatus.Enabled, Is.False);

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete));
});
}

Expand All @@ -62,7 +62,7 @@ public void GetDomainTransferLock(long accountId, string domain, string expected
Assert.That(transferLockStatus.Enabled, Is.True);

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.GET));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Get));
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/dnsimple-test/Services/RegistrarWhoisPrivacyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void EnableWhoisPrivacy(long accountId, string domain, string expectedUrl
Assert.That(privacy.Enabled, Is.True);

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put));
});
}

Expand All @@ -51,7 +51,7 @@ public void PurchaseAndEnableWhoisPrivacy(long accountId, string domain, string
Assert.That(privacy.Enabled, Is.Null);

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.PUT));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Put));
});
}

Expand All @@ -70,7 +70,7 @@ public void DisableWhoisPrivacy(long accountId, string domain, string expectedUr
Assert.That(privacy.Enabled, Is.False);

Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete));
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/dnsimple-test/Services/ServicesDomainsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void ApplyService(long accountId, string domain, string service,

Assert.Multiple(() =>
{
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand All @@ -62,7 +62,7 @@ public void UnapplyService(long accountId, string domain, string service, string

Assert.Multiple(() =>
{
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/dnsimple-test/Services/TemplateDomainsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void ApplyTemplate(long accountId, string domain, string template, string

Assert.Multiple(() =>
{
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/dnsimple-test/Services/TemplateRecordsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void CreateTemplateRecord(long accountId, string template, string expecte
Assert.That(record.Ttl, Is.EqualTo(templateRecord.Ttl));
Assert.That(record.Priority, Is.EqualTo(templateRecord.Priority));

Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.POST));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Post));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand Down Expand Up @@ -168,7 +168,7 @@ public void DeleteTemplateRecord(long accountId, string template, long recordId,

Assert.Multiple(() =>
{
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.DELETE));
Assert.That(client.HttpMethodUsed(), Is.EqualTo(Method.Delete));
Assert.That(client.RequestSentTo(), Is.EqualTo(expectedUrl));
});
}
Expand Down
Loading
Loading