Beispiel #1
0
        public void Test1OneToOne()
        {
            // given
            var bootstrapper = new DefaultNancyBootstrapper();
            var browser      = new Browser(bootstrapper, defaults: to => to.Accept("application/json"));

            ApiBootstrapper.AddOrUpdate("mock", new ApiMock());

            // request simple route.
            var result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "1,1");
                with.Query("loc", "3,4");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.IsNotNull(result);
            var response = OsmSharp.Geo.Streams.GeoJson.GeoJsonConverter.ToFeatureCollection(result.Body.AsString());

            Assert.IsNotNull(response);
            Assert.AreEqual(1, response.Count);

            // request simple route but return full format.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "1,1");
                with.Query("loc", "3,4");
                with.Query("format", "osmsharp");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.IsNotNull(result);
            var route = result.Body.DeserializeJson <OsmSharp.Routing.Route>();

            Assert.IsNotNull(response);
            Assert.AreEqual(1, response.Count);
        }
        public void TestRequestValidation()
        {
            // given
            var bootstrapper = new DefaultNancyBootstrapper();
            var browser      = new Browser(bootstrapper, defaults: to => to.Accept("application/json"));

            ApiBootstrapper.AddOrUpdate("mock", new ApiMock());

            // when incorrect instance request
            var result = browser.Put("notmock/matrix/", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.HttpRequest();
            });

            // then not found
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);

            // when empty request
            result = browser.Put("mock/matrix/", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // only sources but no targets.
            var request = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                sources = new double[2][]
            };

            result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // invalid output options
            request = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                locations = new double[2][],
                output    = new string[]
                {
                    "someinvalidweight"
                }
            };
            result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // invalid output options
            var locations = new double[4][];

            for (var i = 0; i < 4; i++)
            {
                locations[i] = new double[2];
            }
            request = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                locations = locations,
                profile   = new Routing.Matrix.Domain.Profile()
                {
                    vehicle = "pedestrian"
                }
            };
            result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
        }
        public void TestErrors()
        {
            // given
            var bootstrapper = new DefaultNancyBootstrapper();
            var browser      = new Browser(bootstrapper, defaults: to => to.Accept("application/json"));

            ApiBootstrapper.AddOrUpdate("mock", new ApiMock());

            // form an invalid location array.
            var locations = new double[4][];

            for (var i = 0; i < 4; i++)
            {
                locations[i] = new double[2];
            }
            locations[2][1] = 182;
            var request = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                locations = locations,
                output    = new string[]
                {
                    "times"
                }
            };
            var result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // check result.
            Assert.IsNotNull(result);
            var response = result.Body.DeserializeJson <OsmSharp.Service.Routing.Matrix.Domain.Response>();

            Assert.IsNotNull(response);
            Assert.IsNull(response.weights);
            Assert.IsNull(response.distances);
            Assert.IsNotNull(response.times);
            Assert.AreEqual(3, response.times.Length);
            Assert.AreEqual(1, response.errors.Length);
            Assert.AreEqual(2, response.errors[0].index);

            // form an invalid sources and targets array.
            var sources = new double[4][];

            for (var i = 0; i < 4; i++)
            {
                sources[i] = new double[2];
            }
            sources[1][1] = 182;
            var targets = new double[4][];

            for (var i = 0; i < 4; i++)
            {
                targets[i] = new double[2];
            }
            targets[0][1] = 182;
            request       = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                sources = sources,
                targets = targets,
                output  = new string[]
                {
                    "times"
                }
            };
            result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // check result.
            Assert.IsNotNull(result);
            response = result.Body.DeserializeJson <OsmSharp.Service.Routing.Matrix.Domain.Response>();
            Assert.IsNotNull(response);
            Assert.IsNull(response.weights);
            Assert.IsNull(response.distances);
            Assert.IsNotNull(response.times);
            Assert.AreEqual(3, response.times.Length);
            Assert.AreEqual(2, response.errors.Length);
            Assert.AreEqual(1, response.errors[0].index);
            Assert.AreEqual("source", response.errors[0].type);
            Assert.AreEqual(0, response.errors[1].index);
            Assert.AreEqual("target", response.errors[1].type);
        }
        public void TestNxN()
        {
            // given
            var bootstrapper = new DefaultNancyBootstrapper();
            var browser      = new Browser(bootstrapper, defaults: to => to.Accept("application/json"));

            ApiBootstrapper.AddOrUpdate("mock", new ApiMock());

            // form a valid request.
            var locations = new double[4][];

            for (var i = 0; i < 4; i++)
            {
                locations[i] = new double[2];
            }
            var request = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                locations = locations,
                output    = new string[]
                {
                    "times"
                }
            };
            var result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // test response.
            Assert.IsNotNull(result);
            var response = result.Body.DeserializeJson <OsmSharp.Service.Routing.Matrix.Domain.Response>();

            Assert.IsNotNull(response);
            Assert.IsNull(response.weights);
            Assert.IsNull(response.distances);
            Assert.IsNotNull(response.times);
            foreach (var times in response.times)
            {
                foreach (var weight in times)
                {
                    Assert.AreEqual(100, weight);
                }
            }
            request = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                locations = locations,
                output    = new string[]
                {
                    "distances"
                }
            };
            result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // check result.
            Assert.IsNotNull(result);
            response = result.Body.DeserializeJson <OsmSharp.Service.Routing.Matrix.Domain.Response>();
            Assert.IsNotNull(response);
            Assert.IsNull(response.weights);
            Assert.IsNull(response.times);
            Assert.IsNotNull(response.distances);
            foreach (var distances in response.distances)
            {
                foreach (var weight in distances)
                {
                    Assert.AreEqual(100, weight);
                }
            }

            request = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                locations = locations,
                output    = new string[]
                {
                    "weights"
                }
            };
            result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // check result.
            Assert.IsNotNull(result);
            response = result.Body.DeserializeJson <OsmSharp.Service.Routing.Matrix.Domain.Response>();
            Assert.IsNotNull(response);
            Assert.IsNull(response.distances);
            Assert.IsNull(response.times);
            Assert.IsNotNull(response.weights);
            foreach (var weights in response.weights)
            {
                foreach (var weight in weights)
                {
                    Assert.AreEqual(100, weight);
                }
            }

            // default weights.
            request = new OsmSharp.Service.Routing.Matrix.Domain.Request()
            {
                locations = locations
            };
            result = browser.Put("mock/matrix/", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // check result.
            Assert.IsNotNull(result);
            response = result.Body.DeserializeJson <OsmSharp.Service.Routing.Matrix.Domain.Response>();
            Assert.IsNotNull(response);
            Assert.IsNull(response.distances);
            Assert.IsNull(response.weights);
            Assert.IsNotNull(response.times);
            foreach (var times in response.times)
            {
                foreach (var weight in times)
                {
                    Assert.AreEqual(100, weight);
                }
            }
        }
Beispiel #5
0
        public void TestRequestValidation()
        {
            // given
            var bootstrapper = new DefaultNancyBootstrapper();
            var browser      = new Browser(bootstrapper, defaults: to => to.Accept("application/json"));

            ApiBootstrapper.AddOrUpdate("mock", new ApiMock());

            // when incorrect instance request
            var result = browser.Put("notmock/routing/", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.HttpRequest();
            });

            // then not found
            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);

            // when empty request
            result = browser.Get("mock/routing/", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "1;1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect locations.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "car");
                with.Query("loc", "a,1");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with incorrect vehicle.
            result = browser.Get("mock/routing", with =>
            {
                with.Body(string.Empty);
                with.Header("content-type", "application/json");
                with.Query("vehicle", "novehiclehere");
                with.Query("loc", "1,1");
                with.Query("loc", "1,1");
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with 0 locations.
            var request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] { 1, 1 }
                },
                profile = new Domain.Profile()
                {
                    vehicle = "car"
                }
            };

            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with invalid vehicle.
            request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] { 1, 1 },
                    new double[] { 2, 2 }
                },
                profile = new Domain.Profile()
                {
                    vehicle = "novehiclehere"
                }
            };
            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);

            // when request with unsupported vehicle.
            request = new Domain.Request()
            {
                locations = new double[][] {
                    new double[] { 1, 1 },
                    new double[] { 2, 2 }
                },
                profile = new Domain.Profile()
                {
                    vehicle = "pedestrian"
                }
            };
            result = browser.Get("mock/routing", with =>
            {
                with.JsonBody(request);
                with.HttpRequest();
            });

            // then not acceptable
            Assert.AreEqual(HttpStatusCode.NotAcceptable, result.StatusCode);
        }