Skip to content

Allow media type to be specified in release asset upload #1102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ type ListOptions struct {

// UploadOptions specifies the parameters to methods that support uploads.
type UploadOptions struct {
Name string `url:"name,omitempty"`
Name string `url:"name,omitempty"`
MediaType string `url:"-"`
}

// RawType represents type of raw format of a request instead of JSON.
Expand Down
4 changes: 4 additions & 0 deletions github/repos_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ func (s *RepositoriesService) UploadReleaseAsset(ctx context.Context, owner, rep
}

mediaType := mime.TypeByExtension(filepath.Ext(file.Name()))
if opt.MediaType != "" {
mediaType = opt.MediaType
}

req, err := s.client.NewUploadRequest(u, file, stat.Size(), mediaType)
if err != nil {
return nil, nil, err
Expand Down
79 changes: 56 additions & 23 deletions github/repos_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,32 +354,65 @@ func TestRepositoriesService_DeleteReleaseAsset(t *testing.T) {
}

func TestRepositoriesService_UploadReleaseAsset(t *testing.T) {
uploadTests := []struct {
uploadOpts *UploadOptions
fileName string
expectedMediaType string
}{
// No file extension and no explicit media type.
{
&UploadOptions{Name: "n"},
"upload",
defaultMediaType,
},
// File extension and no explicit media type.
{
&UploadOptions{Name: "n"},
"upload.txt",
"text/plain; charset=utf-8",
},
// No file extension and explicit media type.
{
&UploadOptions{Name: "n", MediaType: "image/png"},
"upload",
"image/png",
},
// File extension and explicit media type.
{
&UploadOptions{Name: "n", MediaType: "image/png"},
"upload.png",
"image/png",
},
}

client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/releases/1/assets", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testHeader(t, r, "Content-Type", "text/plain; charset=utf-8")
testHeader(t, r, "Content-Length", "12")
testFormValues(t, r, values{"name": "n"})
testBody(t, r, "Upload me !\n")

fmt.Fprintf(w, `{"id":1}`)
})

file, dir, err := openTestFile("upload.txt", "Upload me !\n")
if err != nil {
t.Fatalf("Unable to create temp file: %v", err)
}
defer os.RemoveAll(dir)
for key, test := range uploadTests {
releaseEndpoint := fmt.Sprintf("/repos/o/r/releases/%d/assets", key)
mux.HandleFunc(releaseEndpoint, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testHeader(t, r, "Content-Type", test.expectedMediaType)
testHeader(t, r, "Content-Length", "12")
testFormValues(t, r, values{"name": "n"})
testBody(t, r, "Upload me !\n")

fmt.Fprintf(w, `{"id":1}`)
})

file, dir, err := openTestFile(test.fileName, "Upload me !\n")
if err != nil {
t.Fatalf("Unable to create temp file: %v", err)
}
defer os.RemoveAll(dir)

opt := &UploadOptions{Name: "n"}
asset, _, err := client.Repositories.UploadReleaseAsset(context.Background(), "o", "r", 1, opt, file)
if err != nil {
t.Errorf("Repositories.UploadReleaseAssert returned error: %v", err)
}
want := &ReleaseAsset{ID: Int64(1)}
if !reflect.DeepEqual(asset, want) {
t.Errorf("Repositories.UploadReleaseAssert returned %+v, want %+v", asset, want)
asset, _, err := client.Repositories.UploadReleaseAsset(context.Background(), "o", "r", int64(key), test.uploadOpts, file)
if err != nil {
t.Errorf("Repositories.UploadReleaseAssert returned error: %v", err)
}
want := &ReleaseAsset{ID: Int64(1)}
if !reflect.DeepEqual(asset, want) {
t.Errorf("Repositories.UploadReleaseAssert returned %+v, want %+v", asset, want)
}
}
}