/
ConnectionTests.cs
166 lines (135 loc) · 6.59 KB
/
ConnectionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Octokit.GraphQL.Core.UnitTests
{
public static class ConnectionTests
{
private static readonly ICredentialStore CredentialStore = new MockCredentialStore();
private static readonly ProductHeaderValue ProductInformation = new ProductHeaderValue("Octokit.GraphQL.Core.Tests", "1.0.0");
[Fact]
public static void Default_GraphQL_Uri_Is_Correct()
{
Assert.Equal("https://api.github.com/graphql", Connection.GithubApiUri?.ToString());
}
[Fact]
public static void Connection_Constructor_Validates_Arguments_For_Null()
{
var productInformation = ProductInformation;
var uri = new Uri("https://api.github.enterprise.local/graphql");
var credentialStore = CredentialStore;
var httpClient = new HttpClient();
Assert.Throws<ArgumentNullException>("productInformation", () => new Connection(null, uri, credentialStore, httpClient));
Assert.Throws<ArgumentNullException>("uri", () => new Connection(productInformation, null, credentialStore, httpClient));
Assert.Throws<ArgumentNullException>("credentialStore", () => new Connection(productInformation, uri, null, httpClient));
Assert.Throws<ArgumentNullException>("httpClient", () => new Connection(productInformation, uri, credentialStore, null));
}
[Fact]
public static void Connection_Constructor_Throws_If_Uri_Is_Relative()
{
var productInformation = ProductInformation;
var uri = new Uri("/graphql", UriKind.Relative);
var credentialStore = CredentialStore;
var exception = Assert.Throws<ArgumentException>("uri", () => new Connection(productInformation, uri, credentialStore));
Assert.StartsWith("The base address for the connection must be an absolute URI.", exception.Message);
}
[Fact]
public static void Connection_Constructors_With_No_Uri_Parameter_Use_GitHub_GraphQL_Uri()
{
var connection = new Connection(ProductInformation, "token");
Assert.Equal(Connection.GithubApiUri, connection.Uri);
connection = new Connection(ProductInformation, CredentialStore);
Assert.Equal(Connection.GithubApiUri, connection.Uri);
connection = new Connection(ProductInformation, CredentialStore, new HttpClient());
Assert.Equal(Connection.GithubApiUri, connection.Uri);
}
[Fact]
public static async Task Run_Specifies_Cancellation_Token()
{
var query = "{}";
var cancellationToken = new CancellationToken(true);
var httpClient = CreateFakeHttpClient(
(request, token) =>
{
Assert.True(token.IsCancellationRequested);
});
var connection = new Connection(ProductInformation, CredentialStore, httpClient);
await Assert.ThrowsAsync<TaskCanceledException>(() => connection.Run(query, cancellationToken));
}
[Theory]
[InlineData("Accept", "application/vnd.github.antiope-preview+json")]
[InlineData("Authorization", "bearer my-token")]
[InlineData("User-Agent", "Octokit.GraphQL.Core.Tests/1.0.0")]
public static async Task Run_Specifies_Http_Headers(string name, string expected)
{
var query = "{}";
var httpClient = CreateFakeHttpClient(
(request, token) =>
{
Assert.Equal(expected, string.Concat(request.Headers.GetValues(name)));
});
var connection = new Connection(ProductInformation, CredentialStore, httpClient);
await connection.Run(query);
}
[Fact]
public static void Run_Throws_If_Query_Is_Null()
{
var connection = new Connection(ProductInformation, CredentialStore);
Assert.ThrowsAsync<ArgumentNullException>("query", () => connection.Run(null));
}
[Fact]
public static async Task Run_Throws_If_Http_Request_Does_Not_Return_An_Http_2xx_Response_Code()
{
var httpClient = CreateFakeHttpClient(statusCode: HttpStatusCode.BadRequest);
var connection = new Connection(ProductInformation, CredentialStore);
var query = "{}";
await Assert.ThrowsAsync<HttpRequestException>(() => connection.Run(query));
}
[Fact]
public static async Task Run_Uses_The_Specified_Uri()
{
var productInformation = ProductInformation;
var uri = new Uri("https://api.github.enterprise.local/graphql");
var credentialStore = CredentialStore;
var query = "{}";
var httpClient = CreateFakeHttpClient(
(request, token) =>
{
Assert.Equal(uri, request.RequestUri);
});
var connection = new Connection(ProductInformation, uri, CredentialStore, httpClient);
await connection.Run(query);
}
private static HttpClient CreateFakeHttpClient(Action<HttpRequestMessage, CancellationToken> inspector = null, HttpStatusCode statusCode = HttpStatusCode.OK)
{
var handler = new MockHttpMessageHandler(inspector, statusCode);
return new HttpClient(handler);
}
private sealed class MockHttpMessageHandler : HttpMessageHandler
{
private readonly Action<HttpRequestMessage, CancellationToken> _inspector;
private readonly HttpStatusCode _statusCode;
public MockHttpMessageHandler(Action<HttpRequestMessage, CancellationToken> inspector, HttpStatusCode statusCode = HttpStatusCode.OK)
{
_inspector = inspector;
_statusCode = statusCode;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
_inspector?.Invoke(request, cancellationToken);
var response = new HttpResponseMessage(_statusCode)
{
Content = new StringContent(string.Empty)
};
return Task.FromResult(response);
}
}
private sealed class MockCredentialStore : ICredentialStore
{
public Task<string> GetCredentials(CancellationToken cancellationToken) => Task.FromResult("my-token");
}
}
}