public async Task AddAttachmentWithNonAsciiFilenameTest() { var nonAsciiFileNames = new List<string> { "Просто", "مرحبا", "你好嗎", "Naïve" }; var responseList = new List<FakeHttpServerResponse>(); for (var i = 0; i < nonAsciiFileNames.Count; i++) { responseList.Add(new FakeHttpServerResponse(201, ResponseMeta)); } var server = new FakeHttpServer(Port, responseList); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); foreach (var fileName in nonAsciiFileNames.Select(fileNamePart => fileNamePart + " file.txt")) { await using var streamWriter = File.CreateText(fileName); streamWriter.AutoFlush = true; await streamWriter.WriteAsync(Guid.NewGuid().ToString()).ConfigureAwait(false); await using var stream = File.OpenRead(fileName); var meta = await storage.AddAttachmentAsync(Country, RecordKey, stream) .ConfigureAwait(false); Assert.IsNotNull(meta); Assert.IsTrue(server.LastRequestBody.Contains($"filename=\"{fileName}\"", StringComparison.InvariantCulture)); File.Delete(fileName); } server.Stop(); }
public void MigrateTestPositive() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, FindResponse), new FakeHttpServerResponse(201, FindResponse), new FakeHttpServerResponse(200, BadFindResponse) }); server.Start(); var config = new StorageConfig(endPoint: "http://*****:*****@ssworD!")); using var storage = Storage.NewStorage(config); const int limit = 50; var migrateResult = storage.MigrateAsync("country", limit).Result; Assert.NotNull(migrateResult); Assert.AreEqual(1, migrateResult.Migrated); Assert.AreEqual(0, migrateResult.TotalLeft); Assert.AreEqual(0, migrateResult.Errors.Count); migrateResult = storage.MigrateAsync("country", limit).Result; Assert.NotNull(migrateResult); Assert.AreEqual(0, migrateResult.Migrated); Assert.AreEqual(1, migrateResult.TotalLeft); Assert.AreEqual(1, migrateResult.Errors.Count); server.Stop(); }
public void FindOneNothingFoundTest() { const string response = "{\n" + " \"data\": [\n" + " ],\n" + " \"meta\": {\n" + " \"count\": 0,\n" + " \"limit\": 100,\n" + " \"offset\": 0,\n" + " \"total\": 0\n" + " }\n" + "}"; var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, response) }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var filter = new FindFilter() .KeyEq(NumberField.Version, 0); var foundedRecord = storage.FindOneAsync("country", filter).Result; Assert.Null(foundedRecord); server.Stop(); }
public void ReadTestPositive() { const string recordKey1 = "123"; const string recordKey2 = "456"; const string body1 = "body1"; const string body2 = "body2"; const string readResponse1 = "{\n" + " \"is_encrypted\": false,\n" + " \"record_key\": \"3E758B0DC7D6A52F2A314FA996BF5513BE8BC5D276935CE2628C67151ABC6DA3\",\n" + " \"body\": \"pt:ewogICJtZXRhIjogewogICAgImlzX2VuY3J5cHRlZCI6IGZhbHNlLAogICAgInJlY29yZF9rZXkiOiAiMTIzIgogIH0sCiAgInBheWxvYWQiOiAiYm9keTEiCn0=\"\n" + "}"; const string readResponse2 = "{\n" + " \"is_encrypted\": false,\n" + " \"record_key\": \"21006B2178B6D5D43CB19FAB2468A679653CDC9EA0280A905F5DCFEBCC0D8CFF\",\n" + " \"body\": \"pt:ewogICJtZXRhIjogewogICAgImlzX2VuY3J5cHRlZCI6IGZhbHNlLAogICAgInJlY29yZF9rZXkiOiAiNDU2IgogIH0sCiAgInBheWxvYWQiOiAiYm9keTIiCn0=\"\n" + "}"; var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, readResponse1), new FakeHttpServerResponse(200, readResponse2) }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var record = storage.ReadAsync("country", "anyRecordKeyForMock").Result; Assert.AreEqual(recordKey1, record.RecordKey); Assert.AreEqual(body1, record.Body); record = storage.ReadAsync("country", "anyRecordKeyForMock").Result; Assert.AreEqual(recordKey2, record.RecordKey); Assert.AreEqual(body2, record.Body); server.Stop(); }
public void GenerateAudienceForTokenClientTest(string endpoint, string endpointMask, FakeHttpServerResponse serverResponse, string midPopAudience, string miniPopAudience) { const string midPopCountryCode = "us"; const string miniPopCountryCode = "pu"; const string recordKey = "someRecordKey"; FakeHttpServer server = null; if (!string.IsNullOrEmpty(serverResponse?.Body)) { server = new FakeHttpServer(Port, new[] { serverResponse }); server.Start(); } using var httpClient = new HttpClient { Timeout = new TimeSpan(0, 0, 30) }; var tokenClient = new FakeTokenClient(); using var dao = HttpDao.NewDao(EnvId, tokenClient, httpClient, endpoint, endpointMask, s_endpoint); //midpop Assert.ThrowsAsync <StorageServerException>(async() => await dao.ReadRecordAsync(midPopCountryCode, recordKey).ConfigureAwait(false)); Assert.AreEqual(midPopAudience, tokenClient.Audience); //minipop Assert.ThrowsAsync <StorageServerException>(async() => await dao.ReadRecordAsync(miniPopCountryCode, recordKey).ConfigureAwait(false)); Assert.AreEqual(miniPopAudience, tokenClient.Audience); server?.Stop(); }
public void DeleteAttachmentNegativeTest() { var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var exception = Assert.ThrowsAsync<StorageClientException>(() => storage.DeleteAttachmentAsync(Country, null, FileId)); Assert.AreEqual("Record key is null", exception.Message); exception = Assert.ThrowsAsync<StorageClientException>(() => storage.DeleteAttachmentAsync(null, RecordKey, FileId)); Assert.AreEqual("Country code is null", exception.Message); exception = Assert.ThrowsAsync<StorageClientException>(() => storage.DeleteAttachmentAsync(Country, RecordKey, null)); Assert.AreEqual("File ID is null", exception.Message); var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(405, "Error") }); server.Start(); var serverException = Assert.ThrowsAsync<StorageServerException>(() => storage.DeleteAttachmentAsync(Country, RecordKey, FileId)); Assert.IsTrue(serverException.Message.Contains( "Unexpected response: StatusCode: 405, ReasonPhrase: 'Method Not Allowed'", StringComparison.InvariantCulture)); server.Stop(); }
static void Main(string[] args) { FakeHttpServer fakeHttpServer = new FakeHttpServer(); fakeHttpServer.Start(); Console.ReadLine(); fakeHttpServer.Stop(); }
public void DefaultParametersTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, CountryLoadResponse) }); server.Start(); using var httpClient = new HttpClient(); using var dao = HttpDao.NewDao("envId", new ApiKeyTokenClient("apiKey"), httpClient, null, "test.localhost", s_endpoint); var popList = dao.GetPopDictionaryClone(); Assert.AreEqual(2, popList.Count); server.Stop(); }
public async Task AddAttachmentTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(201, ResponseMeta), new FakeHttpServerResponse(201, ResponseMeta), new FakeHttpServerResponse(201, ResponseMeta), new FakeHttpServerResponse(201, ResponseMeta), new FakeHttpServerResponse(201, ResponseMeta) }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); await using var writer = GenerateMemoryStream(Guid.NewGuid().ToString()); var meta = await storage.AddAttachmentAsync(Country, RecordKey, writer.BaseStream, FileName, MimeType) .ConfigureAwait(false); Assert.IsNotNull(meta); Assert.AreEqual("POST", server.LastRequestMethod); await using var writer2 = GenerateMemoryStream(Guid.NewGuid().ToString()); meta = await storage.AddAttachmentAsync(Country, RecordKey, writer2.BaseStream, FileName, MimeType, true) .ConfigureAwait(false); Assert.IsNotNull(meta); Assert.AreEqual("PUT", server.LastRequestMethod); await using var writer3 = GenerateMemoryStream(Guid.NewGuid().ToString()); meta = await storage.AddAttachmentAsync(Country, RecordKey, writer3.BaseStream, FileName) .ConfigureAwait(false); Assert.IsNotNull(meta); await using var writer4 = GenerateMemoryStream(Guid.NewGuid().ToString()); meta = await storage.AddAttachmentAsync(Country, RecordKey, writer4.BaseStream) .ConfigureAwait(false); Assert.IsNotNull(meta); Assert.IsTrue(server.LastRequestBody.Contains("filename=\"file\"", StringComparison.InvariantCulture)); await using var streamWriter = File.CreateText(FileName); streamWriter.AutoFlush = true; await streamWriter.WriteAsync(Guid.NewGuid().ToString()).ConfigureAwait(false); await using var stream = File.OpenRead(FileName); meta = await storage.AddAttachmentAsync(Country, RecordKey, stream) .ConfigureAwait(false); Assert.IsNotNull(meta); Assert.IsTrue(server.LastRequestBody.Contains("filename=\"file.txt\"", StringComparison.InvariantCulture)); File.Delete(FileName); server.Stop(); }
public void WriteTestPositive() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(201, "Created") }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); const string recordKey = "123"; var record = storage.WriteAsync("country", new Record(recordKey)).Result; Assert.AreEqual(recordKey, record.RecordKey); server.Stop(); }
public void DeleteTestPositive() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, "OK") }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); const string recordKey = "123"; var result = storage.DeleteAsync("country", recordKey).Result; Assert.True(result); server.Stop(); }
public void GetTokenTest() { const int tokenExpiredLifetime = 1; const string expectedValue1 = "1111111111"; var tokenResponse1 = "{'access_token':'" + expectedValue1 + "' , 'expires_in':'" + tokenExpiredLifetime + "' , 'token_type':'bearer', 'scope':'" + EnvId + "'}"; const string expectedValue2 = "2222222222"; const string tokenResponse2 = "{'access_token':'" + expectedValue2 + "' , 'expires_in':'300' , 'token_type':'bearer', 'scope':'" + EnvId + "'}"; const string expectedValue3 = "3333333333"; const string tokenResponse3 = "{'access_token':'" + expectedValue3 + "' , 'expires_in':'300' , 'token_type':'bearer', 'scope':'" + EnvId + "'}"; var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, tokenResponse1), new FakeHttpServerResponse(200, tokenResponse2), new FakeHttpServerResponse(200, tokenResponse3) }); server.Start(); using var httpClient = new HttpClient(); var tokenClient = new OAuthTokenClient(s_defaultAuthEndpoint, null, EnvId, ClientId, ClientSecret, httpClient); //get the first token var token = tokenClient.RefreshTokenAsync(false, Audience, Apac).Result; Assert.AreEqual(expectedValue1, token); Thread.Sleep(tokenExpiredLifetime * 2 * 1_000); //get new token after expiration token = tokenClient.RefreshTokenAsync(false, Audience, Apac).Result; Assert.AreEqual(expectedValue2, token); //get cashed token token = tokenClient.RefreshTokenAsync(false, Audience, Apac).Result; Assert.AreEqual(expectedValue2, token); //force update token token = tokenClient.RefreshTokenAsync(true, Audience, Apac).Result; Assert.AreEqual(expectedValue3, token); server.Stop(); }
public void TokenResponseWrongStatusTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(401, "") }); server.Start(); using var httpClient = new HttpClient(); var tokenClient = new OAuthTokenClient(s_defaultAuthEndpoint, null, EnvId, ClientId, ClientSecret, httpClient); var exception = Assert.ThrowsAsync <StorageServerException>(async() => { var unused = await tokenClient.RefreshTokenAsync(false, Audience, Emea).ConfigureAwait(false); }); Assert.AreEqual("Unexpected authorization response status='Unauthorized', content=''", exception.Message); server.Stop(); }
public async Task DeleteAttachmentTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(204, "Deleted"), new FakeHttpServerResponse(404, "Not found") }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var status = await storage.DeleteAttachmentAsync(Country, RecordKey, FileId) .ConfigureAwait(false); Assert.IsTrue(status); status = await storage.DeleteAttachmentAsync(Country, RecordKey, FileId) .ConfigureAwait(false); Assert.IsFalse(status); server.Stop(); }
public async Task UpdateAttachmentMetaTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, ResponseMeta), new FakeHttpServerResponse(200, ResponseMeta) }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var meta = await storage.UpdateAttachmentMetaAsync(Country, RecordKey, FileId, FileId) .ConfigureAwait(false); Assert.NotNull(meta); meta = await storage.UpdateAttachmentMetaAsync(Country, RecordKey, FileId, mimeType: MimeType) .ConfigureAwait(false); Assert.NotNull(meta); server.Stop(); }
public void ReadTestNegativeUnexpected() { const string recordKey1 = "123"; const string responseBody1 = "unexpected"; const int responseStatus = 405; var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(responseStatus, responseBody1) }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var exception = Assert.ThrowsAsync <StorageServerException>(() => storage.ReadAsync("country", recordKey1)); Assert.IsTrue(exception.Message.Contains( "Unexpected response: StatusCode: 405, ReasonPhrase: 'Method Not Allowed", StringComparison.InvariantCulture)); server.Stop(); }
public void FindOneTestPositiveTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, FindResponse) }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); const string recordKey = "456"; const string body = "body2"; var filter = new FindFilter() .KeyEq(NumberField.Version, 0); var foundedRecord = storage.FindOneAsync("country", filter).Result; Assert.NotNull(foundedRecord); Assert.AreEqual(body, foundedRecord.Body); Assert.AreEqual(recordKey, foundedRecord.RecordKey); server.Stop(); }
public void BatchWriteTestPositive() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(201, "Created") }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); const string recordKey1 = "123"; const string recordKey2 = "345"; var recordList = new List <Record> { new Record(recordKey1), new Record(recordKey2) }; var records = storage.BatchWriteAsync("country", recordList).Result; Assert.AreEqual(recordKey1, records[0].RecordKey); Assert.AreEqual(recordKey2, records[1].RecordKey); server.Stop(); }
public void UpdateAttachmentMetaNegativeTest() { var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var exception = Assert.ThrowsAsync<StorageClientException>(() => storage.UpdateAttachmentMetaAsync(Country, null, FileId, FileName, MimeType)); Assert.AreEqual("Record key is null", exception.Message); exception = Assert.ThrowsAsync<StorageClientException>(() => storage.UpdateAttachmentMetaAsync(null, RecordKey, FileId, FileName, MimeType)); Assert.AreEqual("Country code is null", exception.Message); exception = Assert.ThrowsAsync<StorageClientException>(() => storage.UpdateAttachmentMetaAsync(Country, RecordKey, null, FileName, MimeType)); Assert.AreEqual("File ID is null", exception.Message); exception = Assert.ThrowsAsync<StorageClientException>(() => storage.UpdateAttachmentMetaAsync(Country, RecordKey, FileId)); Assert.AreEqual("File name and MIME type can't be null at the same time", exception.Message); var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(405, "Error"), new FakeHttpServerResponse(200, "StringNotJson:{[") }); server.Start(); var serverException = Assert.ThrowsAsync<StorageServerException>(() => storage.UpdateAttachmentMetaAsync(Country, RecordKey, FileId, FileName, MimeType)); Assert.IsTrue(serverException.Message.Contains( "Unexpected response: StatusCode: 405, ReasonPhrase: 'Method Not Allowed'", StringComparison.InvariantCulture)); serverException = Assert.ThrowsAsync<StorageServerException>(() => storage.UpdateAttachmentMetaAsync(Country, RecordKey, FileId, FileName, MimeType)); Assert.AreEqual( "Unexpected error " + "[http://localhost:8087/v2/storage/records/us/3e758b0dc7d6a52f2a314fa996bf5513be8bc5d276935ce2628c67151abc6da3/attachments/456/meta]", serverException.Message); Assert.IsNotNull(serverException.InnerException); server.Stop(); }
public void FindNegativeTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(400, "Some error was happened") }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var filter = new FindFilter() .KeyEq(NumberField.Version, 0); var exception = Assert.ThrowsAsync <StorageServerException>(() => storage.FindAsync("country", filter)); Assert.IsNotNull(exception); Assert.IsTrue(exception.Message.Contains( "Unexpected response: StatusCode: 400, ReasonPhrase: 'Bad Request'", StringComparison.InvariantCulture)); Assert.IsTrue(exception.Message.Contains("Some error was happened", StringComparison.InvariantCulture)); server.Stop(); }
public void DeleteTestNegative() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(404, "Not found") }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); const string recordKey = "123"; var result = storage.DeleteAsync("country", recordKey).Result; Assert.False(result); server.Stop(); var exception = Assert.ThrowsAsync <StorageClientException>(() => storage.DeleteAsync("country", null)); Assert.AreEqual("Record key is null", exception.Message); exception = Assert.ThrowsAsync <StorageClientException>(() => storage.DeleteAsync(null, recordKey)); Assert.AreEqual("Country code is null", exception.Message); }
public void ReLoadCountryListTest() { const string shortCountryListJson = "{\n" + " \"countries\": [\n" + " {\n" + " \"direct\": true,\n" + " \"id\": \"US\",\n" + " \"name\": \"United States\",\n" + " \"region\": \"amer\",\n" + " \"status\": \"active\",\n" + " \"type\": \"mid\"\n" + " }]" + "}"; var shortCountryListResponse = new FakeHttpServerResponse(200, shortCountryListJson); var longCountryListResponse = new FakeHttpServerResponse(200, CountryLoadResponse); var server = new FakeHttpServer(Port, new[] { shortCountryListResponse, longCountryListResponse }); using var httpClient = new HttpClient { Timeout = new TimeSpan(0, 0, 1) }; server.Start(); using var dao = HttpDao.NewDao(EnvId, new ApiKeyTokenClient("token"), httpClient, endPointMask: ".localhost", countriesEndPoint: s_endpoint, updateInterval: 1); var popList = dao.GetPopDictionaryClone(); Assert.IsNotNull(popList); Assert.AreEqual(1, popList.Count); Assert.AreEqual("US", popList[0].Id); Thread.Sleep(2_000); Assert.ThrowsAsync <StorageServerException>(async() => await dao.DeleteRecordAsync("us", Guid.NewGuid().ToString()).ConfigureAwait(false)); popList = dao.GetPopDictionaryClone(); Assert.IsNotNull(popList); Assert.AreEqual(2, popList.Count); server.Stop(); }
public async Task GetAttachmentTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, FileId, new Dictionary<string, string> { ["Content-disposition"] = "attachment; filename*=UTF-8''file.txt" }), new FakeHttpServerResponse(200, FileId), new FakeHttpServerResponse(404, "Not found") }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var attachedFile = await storage.GetAttachmentFileAsync(Country, RecordKey, FileId) .ConfigureAwait(false); Assert.IsNotNull(attachedFile); Assert.AreEqual(FileName, attachedFile.Filename); using var reader = new StreamReader(attachedFile.FileContent); var stringFromFile = await reader.ReadToEndAsync().ConfigureAwait(false); Assert.AreEqual(FileId, stringFromFile); attachedFile = await storage.GetAttachmentFileAsync(Country, RecordKey, FileId) .ConfigureAwait(false); Assert.IsNotNull(attachedFile); Assert.IsNull(attachedFile.Filename); using var reader2 = new StreamReader(attachedFile.FileContent); stringFromFile = await reader2.ReadToEndAsync().ConfigureAwait(false); Assert.AreEqual(FileId, stringFromFile); attachedFile = await storage.GetAttachmentFileAsync(Country, RecordKey, FileId) .ConfigureAwait(false); Assert.IsNull(attachedFile); server.Stop(); }
public async Task GetAttachmentMetaTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, ResponseMeta), new FakeHttpServerResponse(404, "Not found") }); server.Start(); var config = new StorageConfig(endPoint: "http://localhost:" + Port, apiKey: "apiKey", environmentId: "envId"); using var storage = Storage.NewStorage(config); var meta = await storage.GetAttachmentMetaAsync(Country, RecordKey, FileId) .ConfigureAwait(false); Assert.AreEqual(FileName, meta.Filename); Assert.AreEqual(FileId, meta.FileId); Assert.AreEqual(44, meta.Size); Assert.AreEqual(MimeType, meta.MimeType); Assert.AreEqual("abc123", meta.Hash); Assert.AreEqual("https://localhost/v2/storage/records/us/123", meta.DownloadLink); Assert.IsTrue(DateTimeOffset.Now > meta.CreatedAt); Assert.IsTrue(DateTimeOffset.Now > meta.UpdatedAt); meta = await storage.GetAttachmentMetaAsync(Country, RecordKey, FileId).ConfigureAwait(false); Assert.IsNull(meta); server.Stop(); }
public void GetEndPointTest() { var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, CountryLoadResponse), //for getting token to midpop new FakeHttpServerResponse(200, TokenResponse), //for getting token to minipop new FakeHttpServerResponse(200, TokenResponse) }); using var httpClient = new HttpClient { Timeout = new TimeSpan(0, 0, 30) }; server.Start(); var tokenClient = new OAuthTokenClient(s_endpoint, null, EnvId, "clientId", "clientSecret", httpClient); using var dao = HttpDao.NewDao(EnvId, tokenClient, httpClient, null, "-localhost:123", s_endpoint); const string midPopCountry = "us"; var exception = Assert.ThrowsAsync <StorageServerException>(async() => await dao.CreateRecordAsync(midPopCountry, new TransferRecord("recordKey")).ConfigureAwait(false)); Assert.AreEqual("Unexpected error [https://us-localhost:123/v2/storage/records/us]", exception.Message); Assert.IsNotNull(exception.InnerException); Assert.IsInstanceOf <HttpRequestException>(exception.InnerException); const string miniPopCountry = "miniPopCountryCode"; exception = Assert.ThrowsAsync <StorageServerException>(async() => await dao.CreateRecordAsync(miniPopCountry, new TransferRecord("recordKey")).ConfigureAwait(false)); Assert.AreEqual("Unexpected error [https://us-localhost:123/v2/storage/records/miniPopCountryCode]", exception.Message); Assert.IsNotNull(exception.InnerException); Assert.IsInstanceOf <HttpRequestException>(exception.InnerException); server.Stop(); }
public async Task AuthorisationRetryTest() { const string tokenValue1 = "12345"; const string tokenResponse1 = "{'access_token':'" + tokenValue1 + "' , 'expires_in':'300' , 'token_type':'bearer', 'scope':'" + EnvId + "'}"; const string tokenValue2 = "67890"; const string tokenResponse2 = "{'access_token':'" + tokenValue2 + "' , 'expires_in':'300' , 'token_type':'bearer', 'scope':'" + EnvId + "'}"; var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, tokenResponse1), new FakeHttpServerResponse(401, "Unauthorized"), new FakeHttpServerResponse(200, tokenResponse2), new FakeHttpServerResponse(201, "OK") }); using var httpClient = new HttpClient(); server.Start(); var tokenClient = new OAuthTokenClient(s_endpoint, null, EnvId, "clientId", "clientSecret", httpClient); using var dao = HttpDao.NewDao(EnvId, tokenClient, httpClient, s_endpoint, ".localhost:" + Port, s_endpoint); const string country = "us"; await dao.CreateRecordAsync(country, new TransferRecord("recordKey")).ConfigureAwait(false); var finalToken = await tokenClient .RefreshTokenAsync(false, s_endpoint + " https://" + country + ".localhost:" + Port, "emea") .ConfigureAwait(false); Assert.AreEqual(tokenValue2, finalToken); server.Stop(); }
public void TokenWrongResponseTest() { const string tokenResponse1 = "{'access_token':'" + "' , 'expires_in':'300' , 'token_type':'bearer', 'scope':'" + EnvId + "'}"; const string tokenResponse2 = "{'access_token':'2222222222'" + ", 'expires_in':'-1' , 'token_type':'bearer', 'scope':'" + EnvId + "'}"; const string tokenResponse3 = "{'access_token':'3333333333'" + ", 'expires_in':'300' , 'token_type':'NotBearer', 'scope':'" + EnvId + "'}"; const string tokenResponse4 = "{'access_token':'3333333333'" + ", 'expires_in':'300' , 'token_type':'bearer', 'scope':'" + EnvId + EnvId + "'}"; const string tokenResponse5 = "{StringNotJson:"; var server = new FakeHttpServer(Port, new[] { new FakeHttpServerResponse(200, tokenResponse1), new FakeHttpServerResponse(200, tokenResponse2), new FakeHttpServerResponse(200, tokenResponse3), new FakeHttpServerResponse(200, tokenResponse4), new FakeHttpServerResponse(200, tokenResponse5) }); server.Start(); using var httpClient = new HttpClient(); var tokenClient = new OAuthTokenClient(s_defaultAuthEndpoint, null, EnvId, ClientId, ClientSecret, httpClient); var exception = Assert.ThrowsAsync <StorageServerException>(async() => { var unused = await tokenClient.RefreshTokenAsync(false, Audience, Emea).ConfigureAwait(false); }); Assert.AreEqual("Token is null", exception.Message); exception = Assert.ThrowsAsync <StorageServerException>(async() => { var unused = await tokenClient.RefreshTokenAsync(false, Audience, Emea).ConfigureAwait(false); }); Assert.AreEqual("Token TTL is invalid", exception.Message); exception = Assert.ThrowsAsync <StorageServerException>(async() => { var unused = await tokenClient.RefreshTokenAsync(false, Audience, Emea).ConfigureAwait(false); }); Assert.AreEqual("Token type is invalid", exception.Message); exception = Assert.ThrowsAsync <StorageServerException>(async() => { var unused = await tokenClient.RefreshTokenAsync(false, Audience, Emea).ConfigureAwait(false); }); Assert.AreEqual("Token scope is invalid", exception.Message); exception = Assert.ThrowsAsync <StorageServerException>(async() => { var unused = await tokenClient.RefreshTokenAsync(false, Audience, Emea).ConfigureAwait(false); }); Assert.AreEqual("Error in parsing authorization response: '{StringNotJson:'", exception.Message); server.Stop(); }