public void TestGetWithQueryStringParameters()
        {
            const string test_key_1 = "testkey1";
            const string test_val_1 = "testval1 that ends with a #";

            const string test_key_2 = "testkey2";
            const string test_val_2 = "testval2 that ends with a space ";

            var request = new JsonWebRequest <HttpBinGetResponse>($@"{default_protocol}://{host}/get")
            {
                Method = HttpMethod.Get,
                AllowInsecureRequests = true
            };

            request.AddParameter(test_key_1, test_val_1);
            request.AddParameter(test_key_2, test_val_2);

            Assert.DoesNotThrow(request.Perform);

            var responseObject = request.ResponseObject;

            Assert.IsTrue(request.Completed);
            Assert.IsFalse(request.Aborted);

            Assert.NotNull(responseObject.Arguments);

            Assert.True(responseObject.Arguments.ContainsKey(test_key_1));
            Assert.AreEqual(test_val_1, responseObject.Arguments[test_key_1]);

            Assert.True(responseObject.Arguments.ContainsKey(test_key_2));
            Assert.AreEqual(test_val_2, responseObject.Arguments[test_key_2]);
        }
Example #2
0
        private void getAccessToken()
        {
            using var req = new JsonWebRequest <dynamic>($"{Program.ENDPOINT_CONFIGURATION.APIEndpointUrl}/oauth/token")
                  {
                      Method = HttpMethod.Post
                  };

            req.AddParameter("client_id", ClientId);
            req.AddParameter("client_secret", ClientSecret);
            req.AddParameter("grant_type", "client_credentials");
            req.AddParameter("scope", "public");
            req.Perform();

            apiAccessToken = req.ResponseObject.access_token.ToString();
        }
        public void TestFormParamsNotSupportedForGet()
        {
            var request = new JsonWebRequest <HttpBinPutResponse>($"{default_protocol}://{host}/get")
            {
                Method = HttpMethod.Get,
                AllowInsecureRequests = true,
            };

            Assert.Throws <ArgumentException>(() => request.AddParameter("cannot", "work", RequestParameterType.Form));
        }
Example #4
0
        public static async Task <bool> VerifyAccessToken(string accessToken)
        {
            var secret = Environment.GetEnvironmentVariable("HCAPTCHA_SECRET");

            if (string.IsNullOrEmpty(secret)) // Ignore if invalid or not
            {
                return(true);
            }

            var webRequest = new JsonWebRequest <HCaptchaResponse>("https://hcaptcha.com/siteverify")
            {
                Method = HttpMethod.Post
            };

            webRequest.AddParameter("response", accessToken);
            webRequest.AddParameter("secret", secret);

            await webRequest.PerformAsync();

            return(webRequest.ResponseObject.Success);
        }
        public void TestPutWithQueryAndFormParams()
        {
            const string test_key_1 = "param1";
            const string test_val_1 = "in query! ";

            const string test_key_2 = "param2";
            const string test_val_2 = "in form!";

            const string test_key_3 = "param3";
            const string test_val_3 = "in form by default!";

            var request = new JsonWebRequest <HttpBinPutResponse>($"{default_protocol}://{host}/put")
            {
                Method = HttpMethod.Put,
                AllowInsecureRequests = true,
            };

            request.AddParameter(test_key_1, test_val_1, RequestParameterType.Query);
            request.AddParameter(test_key_2, test_val_2, RequestParameterType.Form);
            request.AddParameter(test_key_3, test_val_3);

            Assert.DoesNotThrow(request.Perform);

            Assert.IsTrue(request.Completed);
            Assert.IsFalse(request.Aborted);

            var response = request.ResponseObject;

            Assert.NotNull(response.Arguments);
            Assert.True(response.Arguments.ContainsKey(test_key_1));
            Assert.AreEqual(test_val_1, response.Arguments[test_key_1]);

            Assert.NotNull(response.Form);
            Assert.True(response.Form.ContainsKey(test_key_2));
            Assert.AreEqual(test_val_2, response.Form[test_key_2]);

            Assert.NotNull(response.Form);
            Assert.True(response.Form.ContainsKey(test_key_3));
            Assert.AreEqual(test_val_3, response.Form[test_key_3]);
        }
Example #6
0
        public void TestPostWithJsonResponse([Values(true, false)] bool async)
        {
            var request = new JsonWebRequest <HttpBinPostResponse>($"{default_protocol}://{host}/post")
            {
                Method = HttpMethod.Post,
                AllowInsecureRequests = true,
            };

            request.AddParameter("testkey1", "testval1");
            request.AddParameter("testkey2", "testval2");

            if (async)
            {
                Assert.DoesNotThrowAsync(request.PerformAsync);
            }
            else
            {
                Assert.DoesNotThrow(request.Perform);
            }

            var responseObject = request.ResponseObject;

            Assert.IsTrue(request.Completed);
            Assert.IsFalse(request.Aborted);

            Assert.IsTrue(responseObject.Form != null);
            Assert.IsTrue(responseObject.Form.Count == 2);

            Assert.IsTrue(responseObject.Headers.ContentLength > 0);

            Assert.IsTrue(responseObject.Form.ContainsKey("testkey1"));
            Assert.IsTrue(responseObject.Form["testkey1"] == "testval1");

            Assert.IsTrue(responseObject.Form.ContainsKey("testkey2"));
            Assert.IsTrue(responseObject.Form["testkey2"] == "testval2");

            Assert.IsTrue(responseObject.Headers.ContentType.StartsWith("multipart/form-data; boundary="));
        }
Example #7
0
        public void TestPostWithJsonResponse(bool async)
        {
            var request = new JsonWebRequest <HttpBinPostResponse>("https://httpbin.org/post")
            {
                Method = HttpMethod.POST
            };

            request.AddParameter("testkey1", "testval1");
            request.AddParameter("testkey2", "testval2");

            if (async)
            {
                Assert.DoesNotThrowAsync(request.PerformAsync);
            }
            else
            {
                Assert.DoesNotThrow(request.Perform);
            }

            var responseObject = request.ResponseObject;

            Assert.IsTrue(request.Completed);
            Assert.IsFalse(request.Aborted);

            Assert.IsTrue(responseObject.Form != null);
            Assert.IsTrue(responseObject.Form.Count == 2);

            Assert.IsTrue(responseObject.Headers.ContentLength > 0);

            Assert.IsTrue(responseObject.Form.ContainsKey("testkey1"));
            Assert.IsTrue(responseObject.Form["testkey1"] == "testval1");

            Assert.IsTrue(responseObject.Form.ContainsKey("testkey2"));
            Assert.IsTrue(responseObject.Form["testkey2"] == "testval2");

            Assert.IsTrue(responseObject.Headers.ContentType.StartsWith("multipart/form-data; boundary="));
        }