public async void CountryListService_EndpointWorks() { var testApiPath = "/countries"; var mockedData = new CountryListDTO { CountryCollection = new List <CountryDetailsDTO> { new CountryDetailsDTO { TranslatedName = "Norge", Code = "no" } } }; ApiStubHelper.StubServer .Given(Request.Create().WithPath(testApiPath).UsingGet()) .RespondWith( Response.Create() .WithStatusCode(System.Net.HttpStatusCode.OK) .WithBody(Newtonsoft.Json.JsonConvert.SerializeObject(mockedData)) ); var baseService = new BaseWebService(); ApiResponse <CountryListDTO> response = await baseService.Get <CountryListDTO>(ApiStubHelper.StubServerUrl + testApiPath); Assert.True(response.IsSuccessfull); Assert.Equal(mockedData.CountryCollection.Count, response.Data.CountryCollection.Count); Assert.Equal(mockedData.CountryCollection[0].Code, response.Data.CountryCollection[0].Code); Assert.Equal(mockedData.CountryCollection[0].TranslatedName, response.Data.CountryCollection[0].TranslatedName); }
public async Task Get_SendGetRequest_ReturnSuccessResponseWithCorrectTextResult() { var testApiPath = "/testGetText"; var testApiResult = @" { ""Test"": ""Test String"", ""TestBool"": false } "; ApiStubHelper.StubServer .Given(Request.Create().WithPath(testApiPath).UsingGet()) .RespondWith( Response.Create() .WithStatusCode(System.Net.HttpStatusCode.OK) .WithBody(testApiResult) ); var baseService = new BaseWebService(); ApiResponse <TestModelToParse> response = await baseService.Get <TestModelToParse>(ApiStubHelper.StubServerUrl + testApiPath); response.StatusCode.Should().Be((int)HttpStatusCode.OK); response.IsSuccessfull.Should().BeTrue(); response.Data.Test.Should().Be("Test String"); response.Data.TestBool.Should().BeFalse(); }
private void InternalBind(Binding binding, BaseWebService service) { var type = service.GetType(); var name = type.Name; var interfaceTypes = type.GetInterfaces(); var contractType = (Type)null; foreach (var interfaceType in interfaceTypes) { var serviceAttribute = interfaceType.GetCustomAttribute <ServiceContractAttribute>(); if (interfaceType != null) { contractType = interfaceType; break; } } // Should never happen. if (contractType == null) { throw new Exception("Specified service did not have a ServiceContractAttribute."); } //Log.Info($"Binding contract interface {contractInterface.Name}..."); var builder = new UriBuilder(_ctx.ServiceBase); builder.Path = Path.Combine(builder.Path, name); var host = new ServiceHost(service); host.AddServiceEndpoint(contractType, binding, builder.Uri); _hosts.Add(host); }
public async void PostSelvExposureKeys_ShouldInformAboutDeprecatedAPI() { TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(); PrepareMessagingCenter(() => tcs.SetResult(true)); BaseWebService baseWebService = Mock.Of <BaseWebService>( service => service.Post(It.IsAny <SelfDiagnosisSubmissionDTO>(), It.IsAny <string>()) == Task.FromResult( new ApiResponse("", HttpMethod.Post) { Endpoint = "something.zip", Exception = null, Headers = null, ResponseText = null, StatusCode = 410 })); Mock.Get(baseWebService).CallBase = true; bool postSelvExposureKeys = await new ExposureNotificationWebService() .PostSelvExposureKeys( new SelfDiagnosisSubmissionDTO { AppPackageName = "", Keys = new List <ExposureKeyModel>(), Padding = "", Platform = "", Regions = new List <string>(), VisitedCountries = new List <string>() }, new List <ExposureKeyModel>(), baseWebService); using (CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource()) { Task completedTask = await Task.WhenAny(tcs.Task, Task.Delay(2000, timeoutCancellationTokenSource.Token)); if (completedTask == tcs.Task) { timeoutCancellationTokenSource.Cancel(); Assert.True(await tcs.Task); } else { Assert.True(false, "Timeout"); } } Assert.False(postSelvExposureKeys); UnsubscribeMessagingCenter(); }
public static async Task <BaseResponse> ProcessToGetRestaurants() { try { var request = new RestRequest(AppConstants.RestApi.GetRestaurants); var response = await BaseWebService.ExecutePost <BaseResponse>(request); return(response); } catch (Exception e) { Mvx.IoCProvider.Resolve <IAppLogger>().DebugLog(nameof(RestaurantService), e); return(null); } }
public static async Task <InitMenuResponse> ProcessInitRestaurantMenu(RestaurantData selectedRestaurant) { try { var request = new RestRequest(AppConstants.RestApi.InitRestaurantMenu); request.AddParameter("restaurantId", selectedRestaurant.Id); var response = await BaseWebService.ExecuteGet <InitMenuResponse>(request); return(response); } catch (Exception e) { Mvx.IoCProvider.Resolve <IAppLogger>().DebugLog(nameof(ProductService), e); return(null); } }
public static async Task <GetProductByIdResponse> ProcessToGetProductById(long productId) { try { var request = new RestRequest(AppConstants.RestApi.GetProductById); request.AddParameter("productId", productId); //request.AddParameter("productId", 44); var response = await BaseWebService.ExecuteGet <GetProductByIdResponse>(request); return(response); } catch (Exception e) { Mvx.IoCProvider.Resolve <IAppLogger>().DebugLog(nameof(ProductService), e); return(null); } }
public async Task Post_Null_ReturnWrongStatusShouldBeUnsuccessfulAndReturnException() { string testApiPath = "/profiles/withdrawconsent"; ApiStubHelper.StubServer .Given(Request.Create().WithPath(testApiPath).UsingPost()) .RespondWith( Response.Create() .WithStatusCode(0) .WithBodyAsJson("Successfully send POST request") ); BaseWebService baseService = new BaseWebService(); ApiResponse response = await baseService.Post(ApiStubHelper.StubServerUrl + testApiPath); var returnedFailResponseWithException = !response.IsSuccessfull && response.Exception != null; returnedFailResponseWithException.Should().BeTrue(); }
private static async Task <ApiResponse> PostSelfExposureKeysWithAnonTokens(SelfDiagnosisSubmissionDTO selfDiagnosisSubmissionDTO, IEnumerable <ExposureKeyModel> temporaryExposureKeys, BaseWebService service) { var tokenService = new AnonymousTokenService(CustomNamedCurves.GetByOid(X9ObjectIdentifiers.Prime256v1)); var token = await tokenService.GetAnonymousTokenAsync(); var request = new HttpRequestMessage(HttpMethod.Post, Conf.URL_PUT_UPLOAD_DIAGNOSIS_KEYS); request.Headers.Add("Authorization", $"Anonymous {token}"); string jsonBody = JsonConvert.SerializeObject(selfDiagnosisSubmissionDTO, JsonSerializerSettings); request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); var response = await new HttpClient().SendAsync(request); var result = new ApiResponse(Conf.URL_PUT_UPLOAD_DIAGNOSIS_KEYS, HttpMethod.Post); result.StatusCode = (int)response.StatusCode; result.ResponseText = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { result.ResponseText = response.ReasonPhrase; } return(result); }
public async Task <bool> PostSelfExposureKeys(SelfDiagnosisSubmissionDTO selfDiagnosisSubmissionDTO, IEnumerable <ExposureKeyModel> temporaryExposureKeys, BaseWebService service) { ApiResponse response; bool requestedAnonToken; if (AuthenticationState.PersonalData?.AnonymousTokensEnabled == true) { requestedAnonToken = true; response = await PostSelfExposureKeysWithAnonTokens(selfDiagnosisSubmissionDTO, temporaryExposureKeys, service); } else { requestedAnonToken = false; response = await service.Post(selfDiagnosisSubmissionDTO, Conf.URL_PUT_UPLOAD_DIAGNOSIS_KEYS); } // HandleErrorsSilently happens even if IsSuccessfull is true other places in the code, but here // we have an if-else to avoid having to create the redacted key list if we don't have to if (!response.IsSuccessfull) { string redactedKeysJson = RedactedTekListHelper.CreateRedactedTekList(temporaryExposureKeys); HandleErrorsSilently(response, new PostExposureKeysErrorHandler(redactedKeysJson)); } else { HandleErrorsSilently(response); } ENDeveloperToolsViewModel.UpdatePushKeysInfo(response, selfDiagnosisSubmissionDTO, JsonSerializerSettings, requestedAnonToken); return(response.IsSuccessfull); }