Ejemplo n.º 1
0
        public async Task LoginAndInfo_AnyUserWithoutCookies_UnauthorizedOnMeInfo()
        {
            using var _ = TestServerHelper.BuildAndStartHost(Cassette);

            var cookieContainer = new CookieContainer();

            using var httpClient = new HttpClient(HttpMessageHandlerFunc(cookieContainer))
                  {
                      BaseAddress = _baseAddress,
                  };
            var query = QueryHelpers.AddQueryString("/api/users/login", new Dictionary <string, string>
            {
                { "username", "admin" },
                { "password", "password" },
            });
            var response = await httpClient.GetAsync(query);

            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));

            var cookies = cookieContainer.GetCookies(_baseAddress);

            foreach (Cookie cookie in cookies)
            {
                cookie.Expired = true;
            }

            response = await httpClient.GetAsync("/api/users/me/info");

            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
        }
Ejemplo n.º 2
0
        public async Task LoginAndInfo_AnyUserWithCookieContainer_Success()
        {
            using var _ = TestServerHelper.BuildAndStartHost(Cassette);

            var cookieContainer = new CookieContainer();

            using var httpClient = new HttpClient(HttpMessageHandlerFunc(cookieContainer))
                  {
                      BaseAddress = _baseAddress,
                  };
            var query = QueryHelpers.AddQueryString("/api/users/login", new Dictionary <string, string>
            {
                { "username", "admin" },
                { "password", "password" },
            });
            var response = await httpClient.GetAsync(query);

            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));

            Assert.That(cookieContainer.GetCookies(_baseAddress)["value"]?.Value, Is.EqualTo("123"));

            response = await httpClient.GetAsync("/api/users/me/info");

            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
        }
Ejemplo n.º 3
0
        public async Task GetRedirectUsersApi_InvokedOnFirstCallFromCassetteOnSecondCall_Success()
        {
            using var _ = TestServerHelper.BuildAndStartHost();

            var httpClientHandler = new SocketsHttpHandler();
            var innerHandler      = new StubHttpRequestHandler(httpClientHandler);
            var cassette          = new Cassette();
            var tryReplayHandler  = new TryReplayHttpMessageHandler(cassette, innerHandler);

            using var httpClient = new HttpClient(tryReplayHandler)
                  {
                      BaseAddress = _baseAddress
                  };
            var response = await httpClient.GetAsync("/api/get_users/2");

            Assert.That(innerHandler.Invoked, Is.True);
            Assert.That(cassette.Records, Has.Count.EqualTo(1));
            Assert.That(response.RequestMessage.RequestUri, Is.EqualTo(new Uri(_baseAddress, "/api/users/2")));

            innerHandler.Invoked = false;

            response = await httpClient.GetAsync("/api/get_users/2");

            Assert.That(innerHandler.Invoked, Is.False);
            Assert.That(response.RequestMessage?.RequestUri, Is.EqualTo(new Uri(_baseAddress, "/api/users/2")));

            var actual   = JObject.Parse(await response.Content.ReadAsStringAsync());
            var expected = JObject.Parse("{\"id\": 2, \"name\": \"User 2\"}");

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 4
0
        public async Task GetUsersApi_InvokedOnFirstCallFromCassetteOnSecondCall_Success()
        {
            using var _ = TestServerHelper.BuildAndStartHost();

            var httpClientHandler = new SocketsHttpHandler();
            var innerHandler      = new StubHttpRequestHandler(httpClientHandler);
            var cassette          = new Cassette();
            var tryReplayHandler  = new TryReplayHttpMessageHandler(cassette, innerHandler);

            using var httpClient = new HttpClient(tryReplayHandler)
                  {
                      BaseAddress = _baseAddress
                  };
            await httpClient.GetStringAsync("/api/users/1");

            Assert.That(innerHandler.Invoked, Is.True);
            Assert.That(cassette.Records, Has.Count.EqualTo(1));

            innerHandler.Invoked = false;

            var result = await httpClient.GetStringAsync("/api/users/1");

            Assert.That(innerHandler.Invoked, Is.False);

            var actual   = JObject.Parse(result);
            var expected = JObject.Parse("{\"id\": 1, \"name\": \"User 1\"}");

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 5
0
        public async Task Load_GetUser_SuccessReadFromFile()
        {
            using var _ = TestServerHelper.BuildAndStartHost(Cassette);

            using var httpClient = new HttpClient(HttpMessageHandlerFunc(null))
                  {
                      BaseAddress = _baseAddress
                  };
            var user = await httpClient.GetStringAsync("/api/users/1");

            var actual   = JObject.Parse(user);
            var expected = JObject.Parse("{\"id\": 1, \"name\": \"User 1\"}");

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 6
0
        public async Task LoadMultiple_GetUser_SuccessReadFromFile()
        {
            using var _ = TestServerHelper.BuildAndStartHost(Cassette);

            using var httpClient = new HttpClient(HttpMessageHandlerFunc(null))
                  {
                      BaseAddress = _baseAddress
                  };
            var user4 = await httpClient.GetStringAsync("/api/users/4");

            var user5 = await httpClient.GetStringAsync("/api/users/5");

            var actual4   = JObject.Parse(user4);
            var expected4 = JObject.Parse("{\"id\": 4, \"name\": \"User 4\"}");

            Assert.That(actual4, Is.EqualTo(expected4));

            var actual5   = JObject.Parse(user5);
            var expected5 = JObject.Parse("{\"id\": 5, \"name\": \"User 5\"}");

            Assert.That(actual5, Is.EqualTo(expected5));
        }
Ejemplo n.º 7
0
 public static IHost BuildAndStartHost(Cassette cassette) =>
 cassette.Records.Count == 0 ? TestServerHelper.BuildAndStartHost() : null;