From 44e82c5a7abba7d29f280e21d1e5a0a4fae9c35e Mon Sep 17 00:00:00 2001
From: Piotr Zurek
Date: Mon, 21 Mar 2016 17:04:17 +1300
Subject: [PATCH] Fixes path escaping for Repositories.GetContents
Fixes #308.
Fixes #309.
Change-Id: I71d1422c90f32cc6e6834ed428a311a2b2b88778
---
github/repos_contents.go | 4 +---
github/repos_contents_test.go | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/github/repos_contents.go b/github/repos_contents.go
index 6202f19..113d001 100644
--- a/github/repos_contents.go
+++ b/github/repos_contents.go
@@ -128,9 +128,7 @@ func (s *RepositoriesService) DownloadContents(owner, repo, filepath string, opt
//
// 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) {
- // escape characters not allowed in URL path. This actually escapes a
- // lot more, but seems to be harmless.
- escapedPath := url.QueryEscape(path)
+ escapedPath := (&url.URL{Path: path}).String()
u := fmt.Sprintf("repos/%s/%s/contents/%s", owner, repo, escapedPath)
u, err = addOptions(u, opt)
if err != nil {
diff --git a/github/repos_contents_test.go b/github/repos_contents_test.go
index c09775c..d7f8651 100644
--- a/github/repos_contents_test.go
+++ b/github/repos_contents_test.go
@@ -154,6 +154,32 @@ func TestRepositoriesService_GetContents_FilenameNeedsEscape(t *testing.T) {
}
}
+func TestRepositoriesService_GetContents_DirectoryWithSpaces(t *testing.T) {
+ setup()
+ defer teardown()
+ mux.HandleFunc("/repos/o/r/contents/some directory/file.go", func(w http.ResponseWriter, r *http.Request) {
+ testMethod(t, r, "GET")
+ fmt.Fprint(w, `{}`)
+ })
+ _, _, _, err := client.Repositories.GetContents("o", "r", "some directory/file.go", &RepositoryContentGetOptions{})
+ if err != nil {
+ t.Fatalf("Repositories.GetContents returned error: %v", err)
+ }
+}
+
+func TestRepositoriesService_GetContents_DirectoryWithPlusChars(t *testing.T) {
+ setup()
+ defer teardown()
+ mux.HandleFunc("/repos/o/r/contents/some directory+name/file.go", func(w http.ResponseWriter, r *http.Request) {
+ testMethod(t, r, "GET")
+ fmt.Fprint(w, `{}`)
+ })
+ _, _, _, err := client.Repositories.GetContents("o", "r", "some directory+name/file.go", &RepositoryContentGetOptions{})
+ if err != nil {
+ t.Fatalf("Repositories.GetContents returned error: %v", err)
+ }
+}
+
func TestRepositoriesService_GetContents_Directory(t *testing.T) {
setup()
defer teardown()