예제 #1
0
        public static void Init(TestContext testContext)
        {
            var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
            var contentRoot = GetProjectPath(string.Empty, startupAssembly);

            JsonConvert.DefaultSettings = () =>
            {
                var settings = new JsonSerializerSettings();
                settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                settings.Converters.Add(new StringEnumConverter(false));
                return settings;
            };

            TestSetup.TestServer = new TestServer(new WebHostBuilder()
                .UseEnvironment(EnvironmentName.Development)
                .UseContentRoot(contentRoot)
                .ConfigureServices(services => services.AddScoped<DbSeed, TestDbSeed>())
                .UseStartup<Startup>());
            TestSetup.TestServer.BaseAddress = new System.Uri("http://localhost", System.UriKind.Absolute);

            var client = ApiClient.GetClient<AccountClient>().Result;
            for (int i = 0; i < 4; ++i)
            {
                var model = new RegisterBindingModel();

                model.CallbackUrl = client.BaseUrl.ToString();
                model.Language = "de";
                model.Email = i + "test@localhost";
                model.UserName = "******" + i;
                model.Password = "******" + i;
                model.ConfirmPassword = "******" + i;

                try
                {
                    client.RegisterAsync(model).Wait();
                }
                catch(AggregateException e)
                {
                    if (e.InnerExceptions != null && e.InnerExceptions.Count > 0 && e.InnerExceptions[0].GetType() == typeof(ImperaPlusException))
                    {
                        if ((e.InnerExceptions[0] as ImperaPlusException).StatusCode == "400")
                        {
                            // ignore
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
     /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
     /// <returns>Success</returns>
     /// <exception cref="ImperaPlusException">A server side error occurred.</exception>
     public async Task RegisterAsync(RegisterBindingModel model, CancellationToken cancellationToken)
     {
         var url_ = string.Format("{0}/{1}", BaseUrl, "api/Account/Register");
 
         var client_ = await CreateHttpClientAsync(cancellationToken).ConfigureAwait(false);
         var request_ = new HttpRequestMessage();
         PrepareRequest(client_, ref url_);
         var content_ = new StringContent(JsonConvert.SerializeObject(model));
         content_.Headers.ContentType.MediaType = "application/json";
         request_.Content = content_;
         request_.Method = new HttpMethod("POST");
         request_.RequestUri = new Uri(url_, UriKind.RelativeOrAbsolute);
         var response_ = await client_.SendAsync(request_, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false);
         ProcessResponse(client_, response_);
 
         var responseData_ = await response_.Content.ReadAsByteArrayAsync().ConfigureAwait(false); 
         var status_ = ((int)response_.StatusCode).ToString();
 
         if (status_ == "400") 
         {
             var result_ = default(ErrorResponse); 
             try
             {
                 if (responseData_.Length > 0)
                     result_ = JsonConvert.DeserializeObject<ErrorResponse>(Encoding.UTF8.GetString(responseData_, 0, responseData_.Length));                                
             } 
             catch (Exception exception) 
             {
                 throw new ImperaPlusException("Could not deserialize the response body.", status_, responseData_, exception);
             }
             throw new ImperaPlusException<ErrorResponse>("Client Error", status_, responseData_, result_, null);
         }
         else
         if (status_ == "200") 
         {
             return;     
         }
         else
         {
         }
 
         throw new ImperaPlusException("The HTTP status code of the response was not expected (" + (int)response_.StatusCode + ").", status_, responseData_, null);
     }
 /// <returns>Success</returns>
 /// <exception cref="ImperaPlusException">A server side error occurred.</exception>
 public Task RegisterAsync(RegisterBindingModel model)
 {
     return RegisterAsync(model, CancellationToken.None);
 }