public async Task IfContentType_MultipleContentTypesOneIsEqual_ResultReturned() { using var sut = new ApiStub(); sut.Post("/testget", (req, args) => "testresponse") .IfContentType("custom/stubbery") .IfContentType("custom/stubbery2"); sut.Start(); var requestMessage = new HttpRequestMessage(HttpMethod.Post, new UriBuilder(new Uri(sut.Address)) { Path = "/testget" }.Uri) { Content = new StringContent("", Encoding.UTF8, "custom/stubbery") }; var result = await httpClient.SendAsync(requestMessage); var resultString = await result.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.Equal("testresponse", resultString); }
public void NetatmoUnitLoginSuccessFul() { string responseText = "{ \"access_token\":\"544cf4071c7759831d94cdf9|fcb30814afbffd0e39381e74fe38a59a\",\"refresh_token\":\"544cf4071c7759831d94cdf9|2b2c270c1208e8f67d3bd3891e395e1a\",\"scope\":[\"read_station\"],\"expires_in\":10800,\"expire_in\":10800}"; string deviceResponse = netatmoData; ApiStub apiNetatmoStub = new ApiStub(); apiNetatmoStub.Post( "/oauth2/token", (request, args) => { return(responseText); } ); apiNetatmoStub.Get( "/api/devicelist", (request, response) => { return(deviceResponse); } ); apiNetatmoStub.Start(); AppKeyConfig configs = new AppKeyConfig(); configs.NetatmoHost = apiNetatmoStub.Address; netatmoUnit.init(configs); }
public async Task Post_TwoRequestsWithDifferentBodies_BodiesAreDifferent() { using (var sut = new ApiStub()) { sut.Post( "/testpost", (req, args) => $"testresponse body: {args.Body.ReadAsString()}"); sut.Start(); var result1 = await httpClient.PostAsync( new UriBuilder(new Uri(sut.Address)) { Path = "/testpost" }.Uri, new StringContent("orange")); var result2 = await httpClient.PostAsync( new UriBuilder(new Uri(sut.Address)) { Path = "/testpost" }.Uri, new StringContent("pear")); Assert.Equal(HttpStatusCode.OK, result1.StatusCode); Assert.Equal(HttpStatusCode.OK, result2.StatusCode); var resultString1 = await result1.Content.ReadAsStringAsync(); var resultString2 = await result2.Content.ReadAsStringAsync(); Assert.Equal("testresponse body: orange", resultString1); Assert.Equal("testresponse body: pear", resultString2); } }
public void NibeUnit_CurrentReading_Is_SuccessfulWithRefresh() { Assert.True(File.Exists("data/nibeauth.json")); string fileContentExp = "{\"access_token\":\"AAEAAI3RKfqMqpUT6gPct8J4ysi2MvAITg9iJ - fydXOLDEP1DZ_9DbX3hh2Nw3DOydyc_IvBzNJW - k9uon0I4cxNuhhJJ5Z5OQEAUuB6er10aWLS7QnHAxvyAJFgsRUA9BBvmmA21MdBexw4JaKHaIswuxQwsOe3tBefPg8S_eEm44noPJIj_Zge4tgTZyTU1pIj5NksAm0T6i - tnf - wdrPg6AfWZHn1mNBBQnEosPNnq8OatCKK3CUunbCyNspB2v3p175WWSad2stb8Bo1nfvP8ZWebKIQFSYXNYLhYDd3T6EmxqVdIIj_Z6IFeqM4pTRndaIXrPdBCGRvNYCeA5puW2SkAQAAAAEAAB_Tqc5_JXDTzgaTS0amjWGCVvLo5ZV_5lIUMOxBfc7YlrLw0y2qhvUz3GwaMRx5WQdGdHhMkZpxzQPjiN - Zm2KGeyrTwFHj_fXFfxML3Gd_mQF5jrKmRcWwBXqTUDPwdOmPqXR94b6P4PqPzIXuoKvz_MRlrNfA1XmMCKagB8QsAdDmThu7QIR2gV5ENmJUcRHRY09XAAii4kYh6tyhvs8Zec7wgPRZ1Sq6aSOPYwdI3Ux3CRXUPgWxkGBbvCKPIu3keHJoZI - k1U81ha1AD5qj6RqMuM3m72VNXYuzSya62GfNP57BPupfgO_Igv9yWqf7jxPqy_XuAEYF0cNn5hAHDz3Kp9sRieCWW7fiiZwNghVp1 - jo_mdgyd1dPwqsh6UjOJqSuqWSEGWptpxZJ2bnB1akCmqpfKvgJgiV8Ilr68Tjw2uiMPGOlZF3b5T_uRszpTNFIfz4QpWdbbaHabeiBfit4oI3AqsCLEL3MU0W8Sk1QbxikEgON6v - 2lmkJ2t_iUGa3RXh3124QltUujywVVfeEJJupJjs1vRHZmD8\",\"token_type\":\"bearer\",\"expires_in\":1800,\"refresh_token\":\"8_Ln!IAAAAGQC4_hJShmgj10Be6CXXj6SJEJCobqeQMmvBdFp - flssQAAAAGiydTTibPGsB_03OYi - ASktAwRonG9sj0vHJpewUGGmGDxawAXVE4G5mpLHNcpezmDEFg2o3sXIRrdOlymY47itMwqTJyGCSxcoUw3OOVFiMA29VZWSTLjB_hCqMUhTTgxDAo1ykF - kjG - Q84X9xpdx1VEbyBK7LCMYR2h0fcrl0 - qV7MRAhJvKcy7YJ62CXfKm5Nq1PWJ4qTONFYRtL1Z5X8rJ_jLzLYFy3I4EykDqw\",\"scope\":\"READSYSTEM\"}"; ApiStub apiNibeStub = new ApiStub(); apiNibeStub.Post( "/oauth/token", (request, args) => { return(fileContentExp); } ); apiNibeStub.Start(); AppKeyConfig configs = new AppKeyConfig(); configs.NibeHost = apiNibeStub.Address; ClimateItem item = nibeUnit.CurrentReading(configs); Assert.Null(item); }
public async Task IfBody_BodyIsDifferent_NotFoundReturned() { using var sut = new ApiStub(); sut.Post("/testpost", (req, args) => "testresponse") .IfBody(body => body.Contains("testpost")); sut.Start(); var result = await httpClient.PostAsync(new UriBuilder(new Uri(sut.Address)) { Path = "/testpost" }.Uri, new StringContent("testrequest")); Assert.Equal(HttpStatusCode.NotFound, result.StatusCode); }
public async Task Post_CalledDifferentMethod_NotFoundReturned() { using var sut = new ApiStub(); sut.Post("/testpost", (req, args) => "testresponse"); sut.Start(); var result = await httpClient.PutAsync( new UriBuilder(new Uri(sut.Address)) { Path = "/testpost" }.Uri, new StringContent("")); Assert.Equal(HttpStatusCode.NotFound, result.StatusCode); }
public void NetatmoUnitCurrentReading() { string responseText = "{ \"access_token\":\"544cf4071c7759831d94cdf9|fcb30814afbffd0e39381e74fe38a59a\",\"refresh_token\":\"544cf4071c7759831d94cdf9|2b2c270c1208e8f67d3bd3891e395e1a\",\"scope\":[\"read_station\"],\"expires_in\":10800,\"expire_in\":10800}"; //string deviceResponse = File.ReadAllText(fileDir + "netatmodeviceresponse.json"); string deviceResponse = netatmoData; ApiStub apiNetatmoStub = new ApiStub(); apiNetatmoStub.Post( "/oauth2/token", (request, args) => { return(responseText); } ); apiNetatmoStub.Get( "/api/devicelist", (request, response) => { return(deviceResponse); } ); string responseReading = "{\"body\":[{\"beg_time\":1481372100,\"value\":[[0.7]]}],\"status\":\"ok\",\"time_exec\":0.034345865249634,\"time_server\":1481371970}"; apiNetatmoStub.Get( "/api/getmeasure", (request, args) => { return(responseReading); } ); apiNetatmoStub.Start(); AppKeyConfig configs = new AppKeyConfig(); configs.NetatmoHost = apiNetatmoStub.Address; ClimateItem actualReading = netatmoUnit.CurrentReading(configs); Assert.NotNull(actualReading); Assert.Equal("0.7", actualReading.IndoorValue); Assert.Equal("0.7", actualReading.OutdoorValue); Assert.NotNull(actualReading.TimeStamp); Assert.True(DateTime.Now >= actualReading.TimeStamp); }
public async Task Post_CalledSetupRoute_SetupResponseReturned() { using var sut = new ApiStub(); sut.Post("/testpost", (req, args) => "testresponse"); sut.Start(); var result = await httpClient.PostAsync( new UriBuilder(new Uri(sut.Address)) { Path = "/testpost" }.Uri, new StringContent("")); Assert.Equal(HttpStatusCode.OK, result.StatusCode); var resultString = await result.Content.ReadAsStringAsync(); Assert.Equal("testresponse", resultString); }
public async Task IfBody_BodyIsEqual_ResultReturned() { using (var sut = new ApiStub()) { sut.Post("/testpost", (req, args) => "testresponse") .IfBody(body => body.Contains("testpost")); sut.Start(); var result = await httpClient.PostAsync(new UriBuilder(new Uri(sut.Address)) { Path = "/testpost" }.Uri, new StringContent("testpost")); var resultString = await result.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.Equal("testresponse", resultString); } }
public void NetatmoUnitCurrentReadingDataCouldNoBeRead() { string responseText = "{ \"access_token\":\"544cf4071c7759831d94cdf9|fcb30814afbffd0e39381e74fe38a59a\",\"refresh_token\":\"544cf4071c7759831d94cdf9|2b2c270c1208e8f67d3bd3891e395e1a\",\"scope\":[\"read_station\"],\"expires_in\":10800,\"expire_in\":10800}"; //string deviceResponse = File.ReadAllText(fileDir + "netatmodeviceresponse.json"); string deviceResponse = netatmoData; ApiStub apiNetatmoStub = new ApiStub(); apiNetatmoStub.Post( "/oauth2/token", (request, args) => { return(responseText); } ); apiNetatmoStub.Get( "/api/devicelist", (request, response) => { return(deviceResponse); } ); string responseReading = null; apiNetatmoStub.Get( "/api/getmeasure", (request, args) => { return(responseReading); } ); apiNetatmoStub.Start(); AppKeyConfig configs = new AppKeyConfig(); configs.NetatmoHost = apiNetatmoStub.Address; ClimateItem actualReading = netatmoUnit.CurrentReading(configs); Assert.Null(actualReading); }
public async Task Post_BodyPassed_BodyAvailable() { using var sut = new ApiStub(); sut.Post( "/testpost", (req, args) => $"testresponse body: {args.Body.ReadAsString()}"); sut.Start(); var result = await httpClient.PostAsync( new UriBuilder(new Uri(sut.Address)) { Path = "/testpost" }.Uri, new StringContent("orange")); Assert.Equal(HttpStatusCode.OK, result.StatusCode); var resultString = await result.Content.ReadAsStringAsync(); Assert.Equal("testresponse body: orange", resultString); }
public async Task IfContentType_CustomNotMatchingFunc_NotFoundReturned() { using var sut = new ApiStub(); sut.Post("/testget", (req, args) => "testresponse") .IfContentType(contentType => contentType.Contains("does not contain")); sut.Start(); var requestMessage = new HttpRequestMessage(HttpMethod.Post, new UriBuilder(new Uri(sut.Address)) { Path = "/testget" }.Uri) { Content = new StringContent("", Encoding.UTF8, "custom/stubbery") }; var result = await httpClient.SendAsync(requestMessage); Assert.Equal(HttpStatusCode.NotFound, result.StatusCode); }
/// <summary> /// Constructor /// </summary> /// <param name="port">The Port to host the stub on</param> /// <param name="useDefaultResponses">Add the default YNAB stub responses</param> public YnabApiStub(int?port = null, bool useDefaultResponses = true) { _stub = new ApiStub(); if (port.HasValue) { _stub.Port = port; } // Read in default configurations if (useDefaultResponses) { foreach (var fileName in Directory.GetFiles("stub-responses")) { using (var reader = new StreamReader(fileName)) { var endpoint = fileName.Substring(fileName.IndexOf("\\") + 1).Split(".")[0]; endpoint = endpoint.Replace("__", "/"); var content = reader.ReadToEnd(); _stub.Get( $"/{endpoint}", (req, args) => { return(content); } ) .AddDefaultHeaders(); } var postEndpoint = "budgets/14235236-8085-4cf6-9fa6-92c34ed44b0c/transactions"; _stub.Post( $"/{postEndpoint}", (req, args) => { return("{\"data\":{\"transaction_ids\":[\"013a8128-d78f-42ff-a59e-633a4c75253e\"],\"transaction\":{\"id\":\"013a8128-d78f-42ff-a59e-633a4c75253e\",\"date\":\"2019-09-04\",\"amount\":100,\"memo\":\"TEST\",\"cleared\":\"cleared\",\"approved\":true,\"flag_color\":\"red\",\"account_id\":\"09c21bf0-8bd0-4b7b-b158-4fe2df899991\",\"account_name\":\"Checking Account\",\"payee_id\":\"51b1c8ad-f51e-4782-a0dc-f1b2b2cadd61\",\"payee_name\":\"Starting Balance\",\"category_id\":\"6e6c1877-6fa7-461a-8f5f-0aadf361f8cd\",\"category_name\":\"Immediate Income SubCategory\",\"transfer_account_id\":null,\"transfer_transaction_id\":null,\"matched_transaction_id\":null,\"import_id\":null,\"deleted\":false,\"subtransactions\":[]},\"server_knowledge\":174}}"); } ); } } _stub.Start(); }
public void NetatmoUnitLoginFailDueToSetDevice() { string responseText = "{ \"access_token\":\"544cf4071c7759831d94cdf9|fcb30814afbffd0e39381e74fe38a59a\",\"refresh_token\":\"544cf4071c7759831d94cdf9|2b2c270c1208e8f67d3bd3891e395e1a\",\"scope\":[\"read_station\"],\"expires_in\":10800,\"expire_in\":10800}"; ApiStub apiNetatmoStub = new ApiStub(); apiNetatmoStub.Post( "/oauth2/token", (request, args) => { return(responseText); } ); apiNetatmoStub.Start(); AppKeyConfig configs = new AppKeyConfig(); configs.NetatmoHost = apiNetatmoStub.Address; Exception ex = Assert.Throws <Exception>(() => netatmoUnit.init(configs)); Assert.Equal("Could not set device and module", ex.Message); }
public async Task MultiplePosts_OneMatch_DoesntHangConnectionOnLinux() { var largeText = new string('a', 6000); using var sut = new ApiStub(); sut.Post("/", (req, args) => "testresponse1"); sut.Start(); for (var i = 0; i < 5; i++) { var postContent = new StringContent(largeText, Encoding.UTF8, "text/plain"); var result = await httpClient.PostAsync(sut.Address, postContent); Assert.Equal(HttpStatusCode.OK, result.StatusCode); var resultString = await result.Content.ReadAsStringAsync(); Assert.Equal("testresponse1", resultString); } }
public async Task MultipleSetups_OnlyOneMatch_TheMatchingReturned() { using var sut = new ApiStub(); sut.Post("/testget1/{arg1}", (req, args) => $"testresponse,{args.Route.arg2}") .IfHeader("Header1", "TestValue1") .IfHeader("Header2", "TestValue2") .IfContentType("custom/stubbery") .IfAccept("custom/accept") .IfRoute("/testget2/{arg2}") .StatusCode(StatusCodes.Status300MultipleChoices) .Header("ResponseHeader1", "ResponseValue1") .Header("ResponseHeader2", "ResponseValue2"); sut.Start(); var requestMessage = new HttpRequestMessage(HttpMethod.Post, new UriBuilder(new Uri(sut.Address)) { Path = "/testget2/argValue" }.Uri); requestMessage.Headers.Add("Header1", "TestValue1"); requestMessage.Headers.Add("Header2", "TestValue2"); requestMessage.Content = new StringContent("", Encoding.UTF8, "custom/stubbery"); requestMessage.Headers.Add("Accept", "custom/accept"); var result = await httpClient.SendAsync(requestMessage); Assert.Equal(HttpStatusCode.MultipleChoices, result.StatusCode); var resultString = await result.Content.ReadAsStringAsync(); Assert.Equal("testresponse,argValue", resultString); Assert.Equal("ResponseValue1", result.Headers.GetValues("ResponseHeader1").First()); Assert.Equal("ResponseValue2", result.Headers.GetValues("ResponseHeader2").First()); }
public void AddPostRequest(string relativePath, Stubbery.RequestMatching.CreateStubResponse response) { _stub.Post(relativePath, response) .AddDefaultHeaders(); }