Skip to content

Commit

Permalink
Merge pull request #115 from huandu/bug-114-query-string-in-path
Browse files Browse the repository at this point in the history
Fix #114. support query string in path
  • Loading branch information
huandu authored Sep 6, 2018
2 parents 543622e + 5ad8ac6 commit ef41d21
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log #

## v2.3.1 ##

* `[FIX]` [#114](https://github.com/huandu/facebook/pull/114) Query string in the path (e.g. `fb.Get("/me?fields=name,email", nil)`) works as expected now. Thanks, [@AsifArko](https://github.com/AsifArko).

## v2.3.0 ##

* `[FIX]` [#110](https://github.com/huandu/facebook/pull/110) Use HTTP GET to send request in which the method is `GET`. Thank [@nayakravi](https://github.com/nayakravi) for raising this issue, and Thank [@AlphaB](https://github.com/AlphaB) and [@robbiet480](https://github.com/robbiet480) for your valuable inputs.
Expand Down
29 changes: 29 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"
"net/url"
"regexp"
"strings"
)

// Graph API debug mode values.
Expand Down Expand Up @@ -343,6 +344,34 @@ func (session *Session) graph(path string, method Method, params Params) (res Re

session.prepareParams(params)

// parse path only if path contains '?'.
// url.ParseRequestURI cannot parse uri without "/" like "me".
if strings.Contains(path, "?") {
// parse query string in path.
u, e := url.ParseRequestURI(path)

if e != nil {
err = e
return
}

path = u.Path

if u.RawQuery != "" {
query, e := url.ParseQuery(u.RawQuery)

if e != nil {
err = e
return
}

// use these queries to overwrite the value in params.
for k := range query {
params[k] = query.Get(k)
}
}
}

var urlParams Params

if method == GET {
Expand Down
33 changes: 33 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ func TestUploadingBinary(t *testing.T) {
t.Skipf("skip this case as we don't have a valid access token.")
}

t.Skipf("facebook doesn't support uploading photo to timeline.")

buf := bytes.NewBufferString(FB_TEST_BINARY_JPG_FILE)
reader := base64.NewDecoder(base64.StdEncoding, buf)

Expand Down Expand Up @@ -367,3 +369,34 @@ func TestSessionWithCustomBaseUrl(t *testing.T) {
t.Fatal("no call to mock server")
}
}

func TestGetWithQueryString(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}

session := &Session{}
session.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)

id, err := session.User()

if err != nil {
t.Fatalf("cannot get current user id. [e:%v]", err)
}

t.Logf("current user id is %v", id)

result, e := Api("/me?access_token="+FB_TEST_VALID_ACCESS_TOKEN, GET, Params{
"fields": "name,email",
})

if e != nil {
t.Fatalf("cannot get my extended info. [e:%v]", e)
}

if result.Get("name") == nil || result.Get("email") == nil {
t.Fatalf("fail to get my extend info. [result:%v]", result)
}

t.Logf("my extended info is: %v", result)
}

0 comments on commit ef41d21

Please sign in to comment.