Make some of the IResponse properties readonly

This commit is contained in:
Haacked
2015-01-01 21:53:19 -08:00
parent fbfc3fcd2c
commit ff3506f3e8
17 changed files with 148 additions and 185 deletions
@@ -14,11 +14,8 @@ namespace Octokit.Tests.Clients
[Fact]
public async Task RequestsTheEmojiEndpoint()
{
var links = new Dictionary<string, Uri>();
var scopes = new List<string>();
IApiResponse<string> response = new ApiResponse<string>(new Response
{
ApiInfo = new ApiInfo(links, scopes, scopes, "", new RateLimit(new Dictionary<string, string>())),
Body = "<strong>Test</strong>"
}, "<strong>Test</strong>");
var connection = Substitute.For<IConnection>();
@@ -42,18 +39,15 @@ namespace Octokit.Tests.Clients
[Fact]
public async Task RequestsTheEmojiEndpoint()
{
var links = new Dictionary<string, Uri>();
var scopes = new List<string>();
IApiResponse<Dictionary<string, string>> response = new ApiResponse<Dictionary<string, string>>
(new Response
{
ApiInfo = new ApiInfo(links, scopes, scopes, "", new RateLimit(new Dictionary<string, string>()))
},
(
new Response(),
new Dictionary<string, string>
{
{ "foo", "http://example.com/foo.gif" },
{ "bar", "http://example.com/bar.gif" }
});
}
);
var connection = Substitute.For<IConnection>();
connection.Get<Dictionary<string, string>>(Args.Uri, null, null).Returns(Task.FromResult(response));
var client = new MiscellaneousClient(connection);
@@ -16,11 +16,14 @@ namespace Octokit.Tests.Exceptions
[Fact]
public void ParsesRateLimitsFromHeaders()
{
var response = new Response { StatusCode = HttpStatusCode.Forbidden };
response.Headers.Add("X-RateLimit-Limit", "100");
response.Headers.Add("X-RateLimit-Remaining", "42");
response.Headers.Add("X-RateLimit-Reset", "1372700873");
response.ApiInfo = CreateApiInfo(response);
var headers = new Dictionary<string, string>
{
{"X-RateLimit-Limit", "100"},
{"X-RateLimit-Remaining", "42"},
{"X-RateLimit-Reset", "1372700873"}
};
var response = new Response(headers) { StatusCode = HttpStatusCode.Forbidden };
var exception = new RateLimitExceededException(response);
Assert.Equal(HttpStatusCode.Forbidden, exception.StatusCode);
@@ -30,7 +33,6 @@ namespace Octokit.Tests.Exceptions
"Mon 01 Jul 2013 5:47:53 PM -00:00",
"ddd dd MMM yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Assert.Equal("API Rate Limit exceeded", exception.Message);
Assert.Equal(expectedReset, exception.Reset);
}
@@ -38,11 +40,14 @@ namespace Octokit.Tests.Exceptions
[Fact]
public void HandlesInvalidHeaderValues()
{
var response = new Response { StatusCode = HttpStatusCode.Forbidden };
response.Headers.Add("X-RateLimit-Limit", "XXX");
response.Headers.Add("X-RateLimit-Remaining", "XXXX");
response.Headers.Add("X-RateLimit-Reset", "XXXX");
response.ApiInfo = CreateApiInfo(response);
var headers = new Dictionary<string, string>
{
{"X-RateLimit-Limit", "XXX"},
{"X-RateLimit-Remaining", "XXXX"},
{"X-RateLimit-Reset", "XXXX"}
};
var response = new Response(headers) { StatusCode = HttpStatusCode.Forbidden };
var exception = new RateLimitExceededException(response);
Assert.Equal(HttpStatusCode.Forbidden, exception.StatusCode);
@@ -58,11 +63,10 @@ namespace Octokit.Tests.Exceptions
[Fact]
public void HandlesMissingHeaderValues()
{
var response = new Response
var response = new Response(new Dictionary<string, string>())
{
StatusCode = HttpStatusCode.Forbidden
};
response.ApiInfo = CreateApiInfo(response);
var exception = new RateLimitExceededException(response);
Assert.Equal(HttpStatusCode.Forbidden, exception.StatusCode);
@@ -79,11 +83,12 @@ namespace Octokit.Tests.Exceptions
[Fact]
public void CanPopulateObjectFromSerializedData()
{
var response = new Response { StatusCode = HttpStatusCode.Forbidden };
response.Headers.Add("X-RateLimit-Limit", "100");
response.Headers.Add("X-RateLimit-Remaining", "42");
response.Headers.Add("X-RateLimit-Reset", "1372700873");
response.ApiInfo = CreateApiInfo(response);
var headers = new Dictionary<string, string>{
{"X-RateLimit-Limit", "100"},
{"X-RateLimit-Remaining", "42"},
{"X-RateLimit-Reset", "1372700873"}
};
var response = new Response(headers) { StatusCode = HttpStatusCode.Forbidden };
var exception = new RateLimitExceededException(response);
@@ -106,10 +111,5 @@ namespace Octokit.Tests.Exceptions
}
#endif
}
static ApiInfo CreateApiInfo(IResponse response)
{
return new ApiInfo(new Dictionary<string, Uri>(), new List<string>(), new List<string>(), "etag", new RateLimit(response.Headers) );
}
}
}
+1 -6
View File
@@ -87,13 +87,8 @@ namespace Octokit.Tests.Http
public async Task MakesGetRequestForAllItems()
{
var getAllUri = new Uri("anything", UriKind.Relative);
var links = new Dictionary<string, Uri>();
var scopes = new List<string>();
IApiResponse<List<object>> response = new ApiResponse<List<object>>(
new Response
{
ApiInfo = new ApiInfo(links, scopes, scopes, "etag", new RateLimit(new Dictionary<string, string>()))
},
new Response(),
new List<object> { new object(), new object() });
var connection = Substitute.For<IConnection>();
connection.Get<List<object>>(Args.Uri, null, null).Returns(Task.FromResult(response));
+19 -32
View File
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using Octokit.Internal;
using Xunit;
using Xunit.Extensions;
namespace Octokit.Tests
{
@@ -14,21 +13,17 @@ namespace Octokit.Tests
[Fact]
public void ParsesApiInfoFromHeaders()
{
var response = new Response
var headers = new Dictionary<string, string>
{
Headers =
{
{ "X-Accepted-OAuth-Scopes", "user" },
{ "X-OAuth-Scopes", "user, public_repo, repo, gist" },
{ "X-RateLimit-Limit", "5000" },
{ "X-RateLimit-Remaining", "4997" },
{ "ETag", "5634b0b187fd2e91e3126a75006cc4fa" }
}
{ "X-Accepted-OAuth-Scopes", "user" },
{ "X-OAuth-Scopes", "user, public_repo, repo, gist" },
{ "X-RateLimit-Limit", "5000" },
{ "X-RateLimit-Remaining", "4997" },
{ "ETag", "5634b0b187fd2e91e3126a75006cc4fa" }
};
ApiInfoParser.ParseApiHttpHeaders(response);
var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);
var apiInfo = response.ApiInfo;
Assert.NotNull(apiInfo);
Assert.Equal(new[] { "user" }, apiInfo.AcceptedOauthScopes.ToArray());
Assert.Equal(new[] { "user", "public_repo", "repo", "gist" }, apiInfo.OauthScopes.ToArray());
@@ -40,21 +35,17 @@ namespace Octokit.Tests
[Fact]
public void BadHeadersAreIgnored()
{
var response = new Response
var headers = new Dictionary<string, string>
{
Headers =
{
{
"Link",
"<https://api.github.com/repos/rails/rails/issues?page=4&per_page=5>; , " +
"<https://api.github.com/repos/rails/rails/issues?page=131&per_page=5; rel=\"last\""
}
"Link",
"<https://api.github.com/repos/rails/rails/issues?page=4&per_page=5>; , " +
"<https://api.github.com/repos/rails/rails/issues?page=131&per_page=5; rel=\"last\""
}
};
ApiInfoParser.ParseApiHttpHeaders(response);
var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);
var apiInfo = response.ApiInfo;
Assert.NotNull(apiInfo);
Assert.Equal(0, apiInfo.Links.Count);
}
@@ -62,23 +53,19 @@ namespace Octokit.Tests
[Fact]
public void ParsesLinkHeader()
{
var response = new Response
var headers = new Dictionary<string, string>
{
Headers =
{
{
"Link",
"<https://api.github.com/repos/rails/rails/issues?page=4&per_page=5>; rel=\"next\", " +
"<https://api.github.com/repos/rails/rails/issues?page=131&per_page=5>; rel=\"last\", " +
"<https://api.github.com/repos/rails/rails/issues?page=1&per_page=5>; rel=\"first\", " +
"<https://api.github.com/repos/rails/rails/issues?page=2&per_page=5>; rel=\"prev\""
}
"Link",
"<https://api.github.com/repos/rails/rails/issues?page=4&per_page=5>; rel=\"next\", " +
"<https://api.github.com/repos/rails/rails/issues?page=131&per_page=5>; rel=\"last\", " +
"<https://api.github.com/repos/rails/rails/issues?page=1&per_page=5>; rel=\"first\", " +
"<https://api.github.com/repos/rails/rails/issues?page=2&per_page=5>; rel=\"prev\""
}
};
ApiInfoParser.ParseApiHttpHeaders(response);
var apiInfo = ApiInfoParser.ParseResponseHeaders(headers);
var apiInfo = response.ApiInfo;
Assert.NotNull(apiInfo);
Assert.Equal(4, apiInfo.Links.Count);
Assert.Contains("next", apiInfo.Links.Keys);
+5 -6
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
@@ -66,14 +67,12 @@ namespace Octokit.Tests.Http
public async Task ParsesApiInfoOnResponse()
{
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response
var headers = new Dictionary<string, string>
{
Headers =
{
{ "X-Accepted-OAuth-Scopes", "user" },
}
{ "X-Accepted-OAuth-Scopes", "user" },
};
IResponse response = new Response(headers);
httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response));
var connection = new Connection(new ProductHeaderValue("OctokitTests"),
_exampleUri,
@@ -19,11 +19,9 @@ namespace Octokit.Tests.Models
new ApiResponse<List<object>>(new Response(), new List<object> {new object(), new object()}));
var links = new Dictionary<string, Uri> {{"next", nextPageUrl}};
var scopes = new List<string>();
var response = new ApiResponse<List<object>>(new Response
{
ApiInfo = new ApiInfo(links, scopes, scopes, "etag", new RateLimit(new Dictionary<string, string>()))
}, new List<object>());
var httpResponse = Substitute.For<IResponse>();
httpResponse.ApiInfo.Returns(new ApiInfo(links, scopes, scopes, "etag", new RateLimit(new Dictionary<string, string>())));
var response = new ApiResponse<List<object>>(httpResponse, new List<object>());
var connection = Substitute.For<IConnection>();
connection.Get<List<object>>(nextPageUrl, null, null).Returns(nextPageResponse);
var pagedCollection = new ReadOnlyPagedCollection<object>(
@@ -46,18 +46,19 @@ public class ObservableIssuesClientTests
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<Issue>
{
CreateIssue(1),
CreateIssue(2),
CreateIssue(3)
});
}
);
var thirdPageUrl = new Uri("https://example.com/page/3");
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<Issue>
{
CreateIssue(4),
@@ -67,7 +68,7 @@ public class ObservableIssuesClientTests
);
var lastPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<Issue>
{
CreateIssue(7)
@@ -106,7 +107,7 @@ public class ObservableIssuesClientTests
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<Issue>
{
CreateIssue(1),
@@ -118,7 +119,7 @@ public class ObservableIssuesClientTests
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<Issue>
{
CreateIssue(4),
@@ -128,10 +129,10 @@ public class ObservableIssuesClientTests
);
var lastPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<Issue>
{
CreateIssue(7 )
CreateIssue(7)
}
);
var gitHubClient = Substitute.For<IGitHubClient>();
@@ -167,7 +168,7 @@ public class ObservableIssuesClientTests
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<Issue>
{
CreateIssue(1),
@@ -179,7 +180,7 @@ public class ObservableIssuesClientTests
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<Issue>
{
CreateIssue(4),
@@ -189,7 +190,7 @@ public class ObservableIssuesClientTests
);
var lastPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<Issue>
{
CreateIssue(7)
@@ -228,19 +229,19 @@ public class ObservableIssuesClientTests
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<Issue>
{
CreateIssue(1),
CreateIssue(2),
CreateIssue(3),
CreateIssue(3)
}
);
var thirdPageUrl = new Uri("https://example.com/page/3");
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<Issue>
{
CreateIssue(4),
@@ -250,10 +251,10 @@ public class ObservableIssuesClientTests
);
var lastPageResponse = new ApiResponse<List<Issue>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<Issue>
{
CreateIssue(7),
CreateIssue(7)
}
);
var gitHubClient = Substitute.For<IGitHubClient>();
@@ -344,14 +345,17 @@ public class ObservableIssuesClientTests
}
}
static ApiInfo CreateApiInfo(IDictionary<string, Uri> links)
{
return new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>()));
}
static Issue CreateIssue(int issueNumber)
{
var serializer = new SimpleJsonSerializer();
return serializer.Deserialize<Issue>(@"{""number"": """ + issueNumber + @"""}");
}
static IResponse CreateResponseWithApiInfo(IDictionary<string, Uri> links)
{
var apiInfo = new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>()));
var response = Substitute.For<IResponse>();
response.ApiInfo.Returns(apiInfo);
return response;
}
}
@@ -47,7 +47,7 @@ namespace Octokit.Tests.Reactive
var firstPageLinks = new Dictionary<string, Uri> {{"next", secondPageUrl}};
var firstPageResponse = new ApiResponse<List<Milestone>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<Milestone>
{
new Milestone {Number = 1},
@@ -59,7 +59,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> {{"next", thirdPageUrl}};
var secondPageResponse = new ApiResponse<List<Milestone>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<Milestone>
{
new Milestone {Number = 4},
@@ -69,7 +69,7 @@ namespace Octokit.Tests.Reactive
);
var lastPageResponse = new ApiResponse<List<Milestone>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<Milestone>
{
new Milestone {Number = 7},
@@ -100,7 +100,7 @@ namespace Octokit.Tests.Reactive
var firstPageLinks = new Dictionary<string, Uri> {{"next", secondPageUrl}};
var firstPageResponse = new ApiResponse<List<Milestone>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<Milestone>
{
new Milestone {Number = 1},
@@ -112,7 +112,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> {{"next", thirdPageUrl}};
var secondPageResponse = new ApiResponse<List<Milestone>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<Milestone>
{
new Milestone {Number = 4},
@@ -122,7 +122,7 @@ namespace Octokit.Tests.Reactive
);
var lastPageResponse = new ApiResponse<List<Milestone>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<Milestone>
{
new Milestone {Number = 7},
@@ -241,9 +241,11 @@ namespace Octokit.Tests.Reactive
}
}
static ApiInfo CreateApiInfo(IDictionary<string, Uri> links)
static IResponse CreateResponseWithApiInfo(IDictionary<string, Uri> links)
{
return new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>()));
var response = Substitute.For<IResponse>();
response.ApiInfo.Returns(new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>())));
return response;
}
}
}
@@ -12,9 +12,11 @@ namespace Octokit.Tests.Reactive
{
public class ObservablePullRequestReviewCommentsClientTests
{
static ApiInfo CreateApiInfo(IDictionary<string, Uri> links)
static IResponse CreateResponseWithApiInfo(IDictionary<string, Uri> links)
{
return new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>()));
var response = Substitute.For<IResponse>();
response.ApiInfo.Returns(new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>())));
return response;
}
public class TheGetForPullRequestMethod
@@ -27,7 +29,7 @@ namespace Octokit.Tests.Reactive
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 1},
@@ -38,7 +40,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 4},
@@ -48,7 +50,7 @@ namespace Octokit.Tests.Reactive
);
var lastPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 7}
@@ -98,7 +100,7 @@ namespace Octokit.Tests.Reactive
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 1},
@@ -110,7 +112,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 4},
@@ -119,7 +121,7 @@ namespace Octokit.Tests.Reactive
});
var lastPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 7},
@@ -169,7 +171,7 @@ namespace Octokit.Tests.Reactive
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 1},
@@ -181,7 +183,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 4},
@@ -190,7 +192,7 @@ namespace Octokit.Tests.Reactive
});
var lastPageResponse = new ApiResponse<List<PullRequestReviewComment>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<PullRequestReviewComment>
{
new PullRequestReviewComment {Id = 7},
@@ -47,7 +47,7 @@ namespace Octokit.Tests.Reactive
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<PullRequest>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<PullRequest>
{
new PullRequest {Number = 1},
@@ -59,7 +59,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequest>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<PullRequest>
{
new PullRequest {Number = 4},
@@ -69,7 +69,7 @@ namespace Octokit.Tests.Reactive
);
var lastPageResponse = new ApiResponse<List<PullRequest>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<PullRequest>
{
new PullRequest {Number = 7},
@@ -100,7 +100,7 @@ namespace Octokit.Tests.Reactive
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<PullRequest>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<PullRequest>
{
new PullRequest {Number = 1},
@@ -112,7 +112,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequest>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<PullRequest>
{
new PullRequest {Number = 4},
@@ -122,7 +122,7 @@ namespace Octokit.Tests.Reactive
);
var lastPageResponse = new ApiResponse<List<PullRequest>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<PullRequest>
{
new PullRequest {Number = 7},
@@ -282,12 +282,7 @@ namespace Octokit.Tests.Reactive
var connection = Substitute.For<IConnection>();
IApiResponse<List<PullRequestCommit>> response = new ApiResponse<List<PullRequestCommit>>
(
new Response { ApiInfo = new ApiInfo(
new Dictionary<string, Uri>(),
new List<string>(),
new List<string>(),
"",
new RateLimit(new Dictionary<string, string>()))},
new Response(),
new List<PullRequestCommit> { commit }
);
connection.Get<List<PullRequestCommit>>(Args.Uri, null, null)
@@ -324,9 +319,11 @@ namespace Octokit.Tests.Reactive
}
}
static ApiInfo CreateApiInfo(IDictionary<string, Uri> links)
static IResponse CreateResponseWithApiInfo(IDictionary<string, Uri> links)
{
return new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>()));
var response = Substitute.For<IResponse>();
response.ApiInfo.Returns(new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>())));
return response;
}
}
}
@@ -49,7 +49,7 @@ namespace Octokit.Tests.Reactive
var secondPageUrl = new Uri("https://example.com/page/2");
var firstPageLinks = new Dictionary<string, Uri> {{"next", secondPageUrl}};
var firstPageResponse = new ApiResponse<List<Repository>>(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<Repository>
{
new Repository { Id = 1 },
@@ -60,7 +60,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> {{"next", thirdPageUrl}};
var secondPageResponse = new ApiResponse<List<Repository>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<Repository>
{
new Repository {Id = 4},
@@ -68,7 +68,7 @@ namespace Octokit.Tests.Reactive
new Repository {Id = 6}
});
var lastPageResponse = new ApiResponse<List<Repository>>(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<Repository>
{
new Repository { Id = 7 }
@@ -98,7 +98,7 @@ namespace Octokit.Tests.Reactive
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<Repository>>
(
new Response { ApiInfo = CreateApiInfo(firstPageLinks) },
CreateResponseWithApiInfo(firstPageLinks),
new List<Repository>
{
new Repository {Id = 1},
@@ -110,7 +110,7 @@ namespace Octokit.Tests.Reactive
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<Repository>>
(
new Response { ApiInfo = CreateApiInfo(secondPageLinks) },
CreateResponseWithApiInfo(secondPageLinks),
new List<Repository>
{
new Repository {Id = 4},
@@ -123,7 +123,7 @@ namespace Octokit.Tests.Reactive
var thirdPageResponse = new ApiResponse<List<Repository>>
(
new Response { ApiInfo = CreateApiInfo(thirdPageLinks) },
CreateResponseWithApiInfo(thirdPageLinks),
new List<Repository>
{
new Repository {Id = 7}
@@ -131,7 +131,7 @@ namespace Octokit.Tests.Reactive
);
var lastPageResponse = new ApiResponse<List<Repository>>
(
new Response { ApiInfo = CreateApiInfo(new Dictionary<string, Uri>()) },
new Response(),
new List<Repository>
{
new Repository {Id = 8}
@@ -408,9 +408,11 @@ namespace Octokit.Tests.Reactive
}
}
static ApiInfo CreateApiInfo(IDictionary<string, Uri> links)
static IResponse CreateResponseWithApiInfo(IDictionary<string, Uri> links)
{
return new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>()));
var response = Substitute.For<IResponse>();
response.ApiInfo.Returns(new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>())));
return response;
}
}
}
+2 -16
View File
@@ -5,7 +5,7 @@ using System.Text.RegularExpressions;
namespace Octokit.Internal
{
internal static class ApiInfoParser
public static class ApiInfoParser
{
const RegexOptions regexOptions =
#if NETFX_CORE
@@ -17,21 +17,7 @@ namespace Octokit.Internal
static readonly Regex _linkRelRegex = new Regex("rel=\"(next|prev|first|last)\"", regexOptions);
static readonly Regex _linkUriRegex = new Regex("<(.+)>", regexOptions);
public static void ParseApiHttpHeaders(IResponse response)
{
Ensure.ArgumentNotNull(response, "response");
response.ApiInfo = ParseHeaders(response);
}
static ApiInfo ParseHeaders(IResponse response)
{
Ensure.ArgumentNotNull(response, "response");
return ParseResponseHeaders(response.Headers);
}
static ApiInfo ParseResponseHeaders(IDictionary<string, string> responseHeaders)
public static ApiInfo ParseResponseHeaders(IDictionary<string, string> responseHeaders)
{
Ensure.ArgumentNotNull(responseHeaders, "responseHeaders");
+3 -4
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
namespace Octokit.Internal
@@ -16,9 +17,8 @@ namespace Octokit.Internal
var body = response.Body is T ? (T)response.Body : default(T);
HttpResponse = response;
Headers = response.Headers;
Headers = new ReadOnlyDictionary<string, string>(response.Headers);
Body = body;
ResponseUri = response.ResponseUri;
ApiInfo = response.ApiInfo;
StatusCode = response.StatusCode;
ContentType = response.ContentType;
@@ -26,8 +26,7 @@ namespace Octokit.Internal
}
public T Body { get; private set; }
public Dictionary<string, string> Headers { get; private set; }
public Uri ResponseUri { get; set; }
public IReadOnlyDictionary<string, string> Headers { get; private set; }
public ApiInfo ApiInfo { get; set; }
public HttpStatusCode StatusCode { get; set; }
public IResponse HttpResponse { get; private set; }
-1
View File
@@ -432,7 +432,6 @@ namespace Octokit
request.Headers.Add("User-Agent", UserAgent);
await _authenticator.Apply(request).ConfigureAwait(false);
var response = await _httpClient.Send(request, cancellationToken).ConfigureAwait(false);
ApiInfoParser.ParseApiHttpHeaders(response);
HandleErrors(response);
return response;
}
+2 -9
View File
@@ -88,19 +88,12 @@ namespace Octokit.Internal
}
}
var response = new Response
return new Response(responseMessage.Headers.ToDictionary(h => h.Key, h => h.Value.First()))
{
Body = responseBody,
StatusCode = responseMessage.StatusCode,
ContentType = contentType
ContentType = contentType,
};
foreach (var h in responseMessage.Headers)
{
response.Headers.Add(h.Key, h.Value.First());
}
return response;
}
protected virtual HttpRequestMessage BuildRequestMessage(IRequest request)
+4 -5
View File
@@ -18,10 +18,9 @@ namespace Octokit
public interface IResponse
{
object Body { get; }
Dictionary<string, string> Headers { get; }
Uri ResponseUri { get; set; }
ApiInfo ApiInfo { get; set; }
HttpStatusCode StatusCode { get; set; }
string ContentType { get; set; }
IDictionary<string, string> Headers { get; }
ApiInfo ApiInfo { get; }
HttpStatusCode StatusCode { get; }
string ContentType { get; }
}
}
+12 -5
View File
@@ -1,20 +1,27 @@
using System;
using System.Collections.Generic;
using System.Net;
using Octokit.Internal;
namespace Octokit.Internal
{
public class Response : IResponse
{
public Response()
public Response() : this(new Dictionary<string, string>())
{
Headers = new Dictionary<string, string>();
}
public Response(IDictionary<string, string> headers)
{
Ensure.ArgumentNotNull(headers, "headers");
Headers = headers;
ApiInfo = ApiInfoParser.ParseResponseHeaders(headers);
}
public object Body { get; set; }
public Dictionary<string, string> Headers { get; private set; }
public Uri ResponseUri { get; set; }
public ApiInfo ApiInfo { get; set; }
public IDictionary<string, string> Headers { get; private set; }
public ApiInfo ApiInfo { get; private set; }
public HttpStatusCode StatusCode { get; set; }
public string ContentType { get; set; }
}