Skip to content

Commit

Permalink
feat: Add unit test for oasis api
Browse files Browse the repository at this point in the history
issue: #128
  • Loading branch information
CodeBear801 committed Jan 10, 2020
1 parent 0babe83 commit c4bb239
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
57 changes: 57 additions & 0 deletions integration/pkg/api/oasis/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
44 changes: 44 additions & 0 deletions integration/pkg/api/oasis/request_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit c4bb239

Please sign in to comment.