public void TestBasicWebRequest()
        {
            var builder = new HttpWebRequestBuilder()
            {
                BaseUri = UriFactory.Create("http://www.example.com/")
            };

            var wr = builder.GetWebRequest();

            Assert.That(wr.Method, Is.EqualTo("GET"));
            Assert.That(wr.RequestUri.AbsoluteUri, Is.EqualTo("http://www.example.com/"));
        }
        public void TestMultipleQueryParameter()
        {
            var builder = new HttpWebRequestBuilder()
            {
                BaseUri = UriFactory.Create("http://www.example.com/"),
            };
            builder.AddParameter(RequestParameterType.Query, "testQueryParamA", "testValueA");
            builder.AddParameter(RequestParameterType.Query, "testQueryParamB", "testValueB");

            var wr = builder.GetWebRequest();

            Assert.That(wr.Method, Is.EqualTo("GET"));
            Assert.That(wr.RequestUri.AbsoluteUri, Is.EqualTo(
                "http://www.example.com/?testQueryParamA=testValueA&testQueryParamB=testValueB"));
        }
        public void TestMultiplePathParameters()
        {
            var builder = new HttpWebRequestBuilder();

            builder.AddParameter(RequestParameterType.Path, "q", "value1");
            Assert.Throws<ArgumentException>(() =>
            {
                builder.AddParameter(RequestParameterType.Path, "q", "value2");
            });
        }
        public void TestQueryParameterWithUrlEncode()
        {
            var builder = new HttpWebRequestBuilder()
            {
                BaseUri = UriFactory.Create("http://www.example.com/"),
            };

            builder.AddParameter(RequestParameterType.Query, "test Query Param", "test %va/ue");

            var wr = builder.GetWebRequest();

            Assert.That(wr.Method, Is.EqualTo("GET"));
            Assert.That(wr.RequestUri.AbsoluteUri,
                Is.EqualTo("http://www.example.com/?test%20Query%20Param=test%20%25va%2Fue"));
        }
        public void TestPathParameterWithUrlEncode()
        {
            var builder = new HttpWebRequestBuilder()
            {
                BaseUri = UriFactory.Create("http://www.example.com/"),
                Path = "test/{id}",
            };

            builder.AddParameter(RequestParameterType.Path, "id", " %va/ue");
            var wr = builder.GetWebRequest();

            Assert.That(wr.Method, Is.EqualTo("GET"));
            Assert.That(wr.RequestUri.AbsoluteUri, Is.EqualTo("http://www.example.com/test/%20%25va%2Fue"));
        }
        public void TestNullQueryParameters()
        {
            var builder = new HttpWebRequestBuilder()
            {
                BaseUri = new Uri("http://www.example.com"),
                Path = "",
            };

            builder.AddParameter(RequestParameterType.Query, "q", null);
            builder.AddParameter(RequestParameterType.Query, "p", String.Empty);

            Assert.That(builder.BuildUri().AbsoluteUri, Is.EqualTo("http://www.example.com/"));
        }
        public void TestNullPathParameters()
        {
            var builder = new HttpWebRequestBuilder();

            Assert.Throws<ArgumentException>(
                () => builder.AddParameter(RequestParameterType.Path, "q", null));
            Assert.Throws<ArgumentException>(
                () => builder.AddParameter(RequestParameterType.Path, "q", String.Empty));
        }
Exemplo n.º 8
0
        internal HttpWebRequest BuildRequest()
        {
            var requestBuilder = new HttpWebRequestBuilder()
            {
                BaseUri = Service.BaseUri,
                Path = Method.RestPath,
                Method = Method.HttpMethod,
            };

            requestBuilder.AddParameter(RequestParameterType.Query, "alt",
                ReturnType == ReturnType.Json?"json":"atom");

            requestBuilder.AddParameter(RequestParameterType.Query, "key", DeveloperKey);
            requestBuilder.AddParameter(RequestParameterType.Query, "fields", FieldsMask);
            requestBuilder.AddParameter(RequestParameterType.Query, "userIp", UserIp);
            requestBuilder.AddParameter(RequestParameterType.Query, "quotaUser", QuotaUser);

            // Replace the substitution parameters.
            foreach (var parameter in Parameters)
            {
                IParameter parameterDefinition;// = Method.Parameters[parameter.Key];

                if (!(Method.Parameters.TryGetValue(parameter.Key, out parameterDefinition)
                    || Service.Parameters.TryGetValue(parameter.Key, out parameterDefinition)
                    ))
                {
                    throw new GoogleApiException(Service,
                        String.Format("Invalid parameter \"{0}\" specified.", parameter.Key));
                }

                string value = parameter.Value;
                if (value.IsNullOrEmpty()) // If the parameter is present and has no value, use the default.
                {
                    value = parameterDefinition.DefaultValue;
                }
                switch (parameterDefinition.ParameterType)
                {
                    case "path":
                        requestBuilder.AddParameter(RequestParameterType.Path, parameter.Key, value);
                        break;
                    case "query":
                        // If the parameter is optional and no value is given, don't add to url.
                        if (parameterDefinition.IsRequired && String.IsNullOrEmpty(value))
                        {
                            throw new GoogleApiException(Service,
                                String.Format("Required parameter \"{0}\" missing.", parameter.Key));
                        }
                        requestBuilder.AddParameter(RequestParameterType.Query,
                            parameter.Key, value);
                        break;
                    default:
                        throw new NotSupportedException(
                            "Found an unsupported Parametertype [" + parameterDefinition.ParameterType + "]");
                }
            }

            return requestBuilder.GetWebRequest();
        }