Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

test fix

  • Loading branch information...
commit 15495e8603b7f4a57f15c719dab0a568fb463beb 1 parent cc86888
@mrjones authored
Showing with 57 additions and 3 deletions.
  1. +5 −0 README
  2. +23 −3 oauth.go
  3. +29 −0 oauth_test.go
View
5 README
@@ -0,0 +1,5 @@
+# Test
+go test *.go
+
+# Build
+go build *.go
View
26 oauth.go
@@ -276,17 +276,37 @@ func (c *Consumer) Debug(enabled bool) {
c.signer.Debug(enabled)
}
+type pair struct {
+ key string
+ value string
+}
+
+type pairs []pair
+
+func (p pairs) Len() int { return len(p) }
+func (p pairs) Less(i, j int) bool { return p[i].key < p[j].key }
+func (p pairs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+
func (c *Consumer) makeAuthorizedRequest(method string, url string, body string, userParams map[string]string, token *AccessToken) (resp *http.Response, err error) {
allParams := c.baseParams(c.consumerKey, c.AdditionalParams)
allParams.Add(TOKEN_PARAM, token.Token)
authParams := allParams.Clone()
+ // Sort parameters alphabetically (primarily for testability / repeatability)
+ paramPairs := make(pairs, len(userParams))
+ i := 0
+ for key, value := range userParams {
+ paramPairs[i] = pair{key: key, value: value}
+ i++
+ }
+ sort.Sort(paramPairs)
+
queryParams := ""
separator := "?"
if userParams != nil {
- for key, value := range userParams {
- allParams.Add(key, value)
- queryParams += separator + escape(key) + "=" + escape(value)
+ for i := range paramPairs {
+ allParams.Add(paramPairs[i].key, paramPairs[i].value)
+ queryParams += separator + escape(paramPairs[i].key) + "=" + escape(paramPairs[i].value)
separator = "&"
}
}
View
29 oauth_test.go
@@ -292,6 +292,35 @@ func TestCharacterEscaping(t *testing.T) {
assertEq(t, "BODY:SUCCESS", string(body))
}
+
+func TestGetWithNilParams(t *testing.T) {
+ c := basicConsumer()
+ m := newMocks(t)
+ m.install(c)
+
+ m.httpClient.ExpectGet(
+ "http://www.mrjon.es/someurl",
+ nil,
+ "BODY:SUCCESS")
+
+ token := &AccessToken{Token: "TOKEN", Secret: "SECRET"}
+
+ resp, err := c.Get(
+ "http://www.mrjon.es/someurl", nil, token)
+
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ assertEq(t, "consumersecret&SECRET", m.signer.UsedKey)
+
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ t.Fatal(err)
+ }
+ assertEq(t, "BODY:SUCCESS", string(body))
+}
+
func basicConsumer() *Consumer {
return NewConsumer(
"consumerkey",
Please sign in to comment.
Something went wrong with that request. Please try again.