From c4bb239555d433bdd2249aab7ef047e4cc79f1c5 Mon Sep 17 00:00:00 2001 From: CoderBear801 Date: Thu, 9 Jan 2020 16:03:38 -0800 Subject: [PATCH] feat: Add unit test for oasis api issue: https://github.com/Telenav/osrm-backend/issues/128 --- integration/pkg/api/oasis/request.go | 57 +++++++++++++++++++++++ integration/pkg/api/oasis/request_test.go | 44 +++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 integration/pkg/api/oasis/request_test.go diff --git a/integration/pkg/api/oasis/request.go b/integration/pkg/api/oasis/request.go index 4c173436125..b0fd9cccdf4 100644 --- a/integration/pkg/api/oasis/request.go +++ b/integration/pkg/api/oasis/request.go @@ -166,3 +166,60 @@ func floatEquals(a, b float64) bool { func isFloatNegative(a float64) bool { return !floatEquals(math.Abs(a), a) } + +// RequestURI convert RouteRequest to RequestURI (e.g. "/path?foo=bar"). +// see more in https://golang.org/pkg/net/url/#URL.RequestURI +func (r *Request) RequestURI() string { + s := r.pathPrefix() + + coordinatesStr := r.Coordinates.String() + if len(coordinatesStr) > 0 { + s += coordinatesStr + } + + queryStr := r.QueryString() + if len(queryStr) > 0 { + s += api.QuestionMark + queryStr + } + + return s +} + +func (r *Request) pathPrefix() string { + //i.e. "/oasis/v1/earliest/" + return api.Slash + r.Service + api.Slash + r.Version + api.Slash + r.Profile + api.Slash +} + +// QueryString convert RouteRequest to "URL encoded" form ("bar=baz&foo=quux"), but NOT escape. +func (r *Request) QueryString() string { + rawQuery := r.QueryValues().Encode() + query, err := url.QueryUnescape(rawQuery) + if err != nil { + glog.Warning(err) + return rawQuery // use rawQuery if unescape fail + } + return query +} + +// QueryValues convert route Request to url.Values. +func (r *Request) QueryValues() (v url.Values) { + v = make(url.Values) + + if r.MaxRange != 0 { + v.Add(options.KeyMaxRange, strconv.FormatFloat(r.MaxRange, 'f', -1, 64)) + } + + if r.CurrRange != 0 { + v.Add(options.KeyCurrRange, strconv.FormatFloat(r.CurrRange, 'f', -1, 64)) + } + + if r.PreferLevel != 0 { + v.Add(options.KeyPreferLevel, strconv.FormatFloat(r.PreferLevel, 'f', -1, 64)) + } + + if r.SafeLevel != 0 { + v.Add(options.KeySafeLevel, strconv.FormatFloat(r.SafeLevel, 'f', -1, 64)) + } + + return +} diff --git a/integration/pkg/api/oasis/request_test.go b/integration/pkg/api/oasis/request_test.go new file mode 100644 index 00000000000..0444ab6f29c --- /dev/null +++ b/integration/pkg/api/oasis/request_test.go @@ -0,0 +1,44 @@ +package oasis + +import ( + "testing" + + "github.com/Telenav/osrm-backend/integration/pkg/api/osrm/coordinate" +) + +func TestOasisRequestURI(t *testing.T) { + cases := []struct { + r Request + expect string + }{ + { + Request{ + Service: "oasis", + Version: "v1", + Profile: "earliest", + Coordinates: coordinate.Coordinates{coordinate.Coordinate{Lat: 37.364336, Lon: -122.006349}, coordinate.Coordinate{Lat: 37.313767, Lon: -121.875654}}, + }, + "/oasis/v1/earliest/-122.006349,37.364336;-121.875654,37.313767", + }, + { + Request{ + Service: "oasis", + Version: "v1", + Profile: "earliest", + Coordinates: coordinate.Coordinates{coordinate.Coordinate{Lat: 37.364336, Lon: -122.006349}, coordinate.Coordinate{Lat: 37.313767, Lon: -121.875654}}, + MaxRange: 300.1, + CurrRange: 100, + PreferLevel: 80.0, + SafeLevel: 50.0, + }, + "/oasis/v1/earliest/-122.006349,37.364336;-121.875654,37.313767?curr_range=100&max_range=300.1&prefer_level=80&safe_level=50", + }, + } + + for _, c := range cases { + s := c.r.RequestURI() + if s != c.expect { + t.Errorf("%v QueryString(), expect %s, but got %s", c.r, c.expect, s) + } + } +}