Browse Source

Do URL path escape composing the URL for GetContents

David Deng 10 years ago
committed by Will Norris
parent
commit
d3fb171e8b
2 changed files with 18 additions and 3 deletions
  1. +5
    -3
      github/repos_contents.go
  2. +13
    -0
      github/repos_contents_test.go

+ 5
- 3
github/repos_contents.go View File

@ -127,9 +127,11 @@ func (s *RepositoriesService) DownloadContents(owner, repo, filepath string, opt
// value and the other will be nil.
//
// GitHub API docs: http://developer.github.com/v3/repos/contents/#get-contents
func (s *RepositoriesService) GetContents(owner, repo, path string, opt *RepositoryContentGetOptions) (fileContent *RepositoryContent,
directoryContent []*RepositoryContent, resp *Response, err error) {
u := fmt.Sprintf("repos/%s/%s/contents/%s", owner, repo, path)
func (s *RepositoriesService) GetContents(owner, repo, path string, opt *RepositoryContentGetOptions) (fileContent *RepositoryContent, directoryContent []*RepositoryContent, resp *Response, err error) {
// escape characters not allowed in URL path. This actually escapes a
// lot more, but seems to be harmless.
escapedPath := url.QueryEscape(path)
u := fmt.Sprintf("repos/%s/%s/contents/%s", owner, repo, escapedPath)
u, err = addOptions(u, opt)
if err != nil {
return nil, nil, nil, err


+ 13
- 0
github/repos_contents_test.go View File

@ -141,6 +141,19 @@ func TestRepositoriesService_GetContents_File(t *testing.T) {
}
}
func TestRepositoriesService_GetContents_FilenameNeedsEscape(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/repos/o/r/contents/p#?%/中.go", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{}`)
})
_, _, _, err := client.Repositories.GetContents("o", "r", "p#?%/中.go", &RepositoryContentGetOptions{})
if err != nil {
t.Fatalf("Repositories.GetContents returned error: %v", err)
}
}
func TestRepositoriesService_GetContents_Directory(t *testing.T) {
setup()
defer teardown()


Loading…
Cancel
Save