public UserControllerTest(UserWebApplicationFactory <UserService.Startup> factory, AuthWebApplicationFactory <AuthenticationService.Startup> authFactory) { //calling Auth API to get JWT AuthenticationService.Models.User user = new AuthenticationService.Models.User { UserId = "Mukesh", Password = "******" }; _authclient = authFactory.CreateClient(); HttpRequestMessage request = new HttpRequestMessage(); MediaTypeFormatter formatter = new JsonMediaTypeFormatter(); // The endpoint or route of the controller action. var httpResponse = _authclient.PostAsync <AuthenticationService.Models.User>("/api/auth/login", user, formatter); httpResponse.Wait(); // Deserialize and examine results. var stringResponse = httpResponse.Result.Content.ReadAsStringAsync(); var response = JsonConvert.DeserializeObject <TokenModel>(stringResponse.Result); _client = factory.CreateClient(); //Attaching token in request header _client.DefaultRequestHeaders.Add("Authorization", $"Bearer {response.Token}"); }
public UserControllerTest(UserWebApplicationFactory <Startup> factory) { _client = factory.CreateClient(); }