Created
October 23, 2019 18:29
-
-
Save dvliman/8b083ece1022957efa6d29f5d6a4d00d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package httplib | |
import ( | |
"net/http" | |
"net/http/httptest" | |
"net/url" | |
"testing" | |
"github.com/manyminds/api2go" | |
"github.com/stretchr/testify/assert" | |
) | |
type Dummy struct{} | |
func (d Dummy) GetID() string { | |
return "dummies" | |
} | |
func (d Dummy) FindOne(id string, r api2go.Request) (api2go.Responder, error) { | |
// api2go -> api.go -> buildRequest(c APIContexter, r *http.Request) is private :( | |
// inspect 'r' to make sure we properly construct api2go.Request{} | |
return resource.Response{Res: Dummy{}, Code: http.StatusOK}, nil | |
} | |
func TestDummy(t *testing.T) { | |
api := api2go.NewAPI("api") | |
api.AddResource(Dummy{}, Dummy{}) | |
//req := httptest.NewRequest("GET", "/api/dummies/123?page[number]=10&page[size]=20&page[limit]=30&page[offset]=40", nil) | |
//req := httptest.NewRequest("GET", "/api/dummies/123?fields[acquisitions]=id,status,created", nil) | |
//req := httptest.NewRequest("GET", "/api/dummies/123?sort=-created,updated", nil) | |
req := httptest.NewRequest("GET", "/api/dummies/123?a=b&a=c", nil) | |
res := httptest.NewRecorder() | |
api.Handler().ServeHTTP(res, req) | |
} | |
func TestDuplicateQueryStringKey(t *testing.T) { | |
// from http.Request standpoint, it will collect values from 'duplicate' query string key | |
values1, _ := url.ParseQuery("a=b&a=c") | |
assert.Equal(t, url.Values{"a": []string{"b", "c"}}, values1) | |
req2, _ := http.NewRequest("GET", "/api/dummies/123?a=b&a=c", nil) | |
values2 := req2.URL.Query() | |
assert.Equal(t, url.Values{"a": []string{"b", "c"}}, values2) | |
// there is a bug in api2go, see api.go#buildRequest() line 507-508 | |
// | |
// for key, values := range r.URL.Query() { | |
// params[key] = strings.Split(values[0], ",") | |
// | |
// it should be: | |
// for key, values := range r.URL.Query() { | |
// foreach value in values: | |
// strings.Split(value) rather than always v[0] | |
// append each split-result into params[key] | |
} | |
© 2019 GitHub, Inc. | |
Terms | |
Privacy | |
Security | |
Status | |
Help | |
Contact GitHub | |
Pricing | |
API | |
Training | |
Blog | |
About |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment