Skip to content

Commit

Permalink
Use url.QueryEscape instead of url.PathEscape to escape query string (#…
Browse files Browse the repository at this point in the history
…1125)

Fixes #1122.
  • Loading branch information
Yosuke Akatsuka authored and gmlewis committed Feb 28, 2019
1 parent d173fe3 commit 7462feb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
13 changes: 5 additions & 8 deletions github/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ package github
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"

qs "github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -218,20 +216,19 @@ func (s *SearchService) Labels(ctx context.Context, repoID int64, query string,

// Helper function that executes search queries against different
// GitHub search types (repositories, commits, code, issues, users, labels)
//
// If searchParameters.Query includes multiple condition, it MUST NOT include "+" as condition separator.
// For example, querying with "language:c++" and "leveldb", then searchParameters.Query should be "language:c++ leveldb" but not "language:c+++leveldb".
func (s *SearchService) search(ctx context.Context, searchType string, parameters *searchParameters, opt *SearchOptions, result interface{}) (*Response, error) {
params, err := qs.Values(opt)
if err != nil {
return nil, err
}
q := strings.Replace(parameters.Query, " ", "+", -1)
if parameters.RepositoryID != nil {
params.Set("repository_id", strconv.FormatInt(*parameters.RepositoryID, 10))
}
query := "q=" + url.PathEscape(q)
if v := params.Encode(); v != "" {
query = query + "&" + v
}
u := fmt.Sprintf("search/%s?%s", searchType, query)
params.Set("q", parameters.Query)
u := fmt.Sprintf("search/%s?%s", searchType, params.Encode())

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions github/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestSearchService_Issues_withQualifiersNoOpts(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

const q = "gopher is:issue label:bug language:go pushed:>=2018-01-01 stars:>=200"
const q = "gopher is:issue label:bug language:c++ pushed:>=2018-01-01 stars:>=200"

var requestURI string
mux.HandleFunc("/search/issues", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -134,7 +134,7 @@ func TestSearchService_Issues_withQualifiersNoOpts(t *testing.T) {
t.Errorf("Search.Issues returned error: %v", err)
}

if want := "/api-v3/search/issues?q=gopher+is:issue+label:bug+language:go+pushed:%3E=2018-01-01+stars:%3E=200"; requestURI != want {
if want := "/api-v3/search/issues?q=gopher+is%3Aissue+label%3Abug+language%3Ac%2B%2B+pushed%3A%3E%3D2018-01-01+stars%3A%3E%3D200"; requestURI != want {
t.Fatalf("URI encoding failed: got %v, want %v", requestURI, want)
}

Expand All @@ -152,7 +152,7 @@ func TestSearchService_Issues_withQualifiersAndOpts(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

const q = "gopher is:issue label:bug language:go pushed:>=2018-01-01 stars:>=200"
const q = "gopher is:issue label:bug language:c++ pushed:>=2018-01-01 stars:>=200"

var requestURI string
mux.HandleFunc("/search/issues", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -172,7 +172,7 @@ func TestSearchService_Issues_withQualifiersAndOpts(t *testing.T) {
t.Errorf("Search.Issues returned error: %v", err)
}

if want := "/api-v3/search/issues?q=gopher+is:issue+label:bug+language:go+pushed:%3E=2018-01-01+stars:%3E=200&sort=forks"; requestURI != want {
if want := "/api-v3/search/issues?q=gopher+is%3Aissue+label%3Abug+language%3Ac%2B%2B+pushed%3A%3E%3D2018-01-01+stars%3A%3E%3D200&sort=forks"; requestURI != want {
t.Fatalf("URI encoding failed: got %v, want %v", requestURI, want)
}

Expand Down

0 comments on commit 7462feb

Please sign in to comment.