Browse Source

add RepoContent.GetContent to replace Decode

Decode only works if r.Content is actually encoded, which I'm not sure
that it always will be.  GetContent will return the content, decoding it
only if necessary.  Deprecate the old Decode method.

Also expand and cleanup the tests for both of these methods.

Related to #323
Will Norris 10 years ago
parent
commit
d5f0be217b
2 changed files with 104 additions and 17 deletions
  1. +23
    -0
      github/repos_contents.go
  2. +81
    -17
      github/repos_contents_test.go

+ 23
- 0
github/repos_contents.go View File

@ -61,6 +61,8 @@ func (r RepositoryContent) String() string {
}
// Decode decodes the file content if it is base64 encoded.
//
// Deprecated: Use GetContent instead.
func (r *RepositoryContent) Decode() ([]byte, error) {
if *r.Encoding != "base64" {
return nil, errors.New("cannot decode non-base64")
@ -72,6 +74,27 @@ func (r *RepositoryContent) Decode() ([]byte, error) {
return o, nil
}
// GetContent returns the content of r, decoding it if necessary.
func (r *RepositoryContent) GetContent() (string, error) {
var encoding string
if r.Encoding != nil {
encoding = *r.Encoding
}
switch encoding {
case "base64":
c, err := base64.StdEncoding.DecodeString(*r.Content)
return string(c), err
case "":
if r.Content == nil {
return "", nil
}
return *r.Content, nil
default:
return "", fmt.Errorf("unsupported content encoding: %v", encoding)
}
}
// GetReadme gets the Readme file for the repository.
//
// GitHub API docs: http://developer.github.com/v3/repos/contents/#get-the-readme


+ 81
- 17
github/repos_contents_test.go View File

@ -8,27 +8,91 @@ import (
"testing"
)
func TestDecode(t *testing.T) {
setup()
defer teardown()
r := RepositoryContent{Encoding: String("base64"), Content: String("aGVsbG8=")}
o, err := r.Decode()
if err != nil {
t.Errorf("Failed to decode content.")
func TestRepositoryContent_Decode(t *testing.T) {
tests := []struct {
encoding, content *string // input encoding and content
want string // desired output
wantErr bool // whether an error is expected
}{
{
encoding: String("base64"),
content: String("aGVsbG8="),
want: "hello",
wantErr: false,
},
{
encoding: String("bad"),
content: String("aGVsbG8="),
want: "",
wantErr: true,
},
}
want := "hello"
if string(o) != want {
t.Errorf("RepositoryContent.Decode returned %+v, want %+v", string(o), want)
for _, tt := range tests {
r := RepositoryContent{Encoding: tt.encoding, Content: tt.content}
o, err := r.Decode()
if err != nil && !tt.wantErr {
t.Errorf("RepositoryContent(%q, %q) returned unexpected error: %v", tt.encoding, tt.content, err)
}
if err == nil && tt.wantErr {
t.Errorf("RepositoryContent(%q, %q) did not return unexpected error", tt.encoding, tt.content)
}
if got, want := string(o), tt.want; got != want {
t.Errorf("RepositoryContent.Decode returned %+v, want %+v", got, want)
}
}
}
func TestDecodeBadEncoding(t *testing.T) {
setup()
defer teardown()
r := RepositoryContent{Encoding: String("bad")}
_, err := r.Decode()
if err == nil {
t.Errorf("Should fail to decode non-base64")
func TestRepositoryContent_GetContent(t *testing.T) {
tests := []struct {
encoding, content *string // input encoding and content
want string // desired output
wantErr bool // whether an error is expected
}{
{
encoding: String(""),
content: String("hello"),
want: "hello",
wantErr: false,
},
{
encoding: nil,
content: String("hello"),
want: "hello",
wantErr: false,
},
{
encoding: nil,
content: nil,
want: "",
wantErr: false,
},
{
encoding: String("base64"),
content: String("aGVsbG8="),
want: "hello",
wantErr: false,
},
{
encoding: String("bad"),
content: String("aGVsbG8="),
want: "",
wantErr: true,
},
}
for _, tt := range tests {
r := RepositoryContent{Encoding: tt.encoding, Content: tt.content}
got, err := r.GetContent()
if err != nil && !tt.wantErr {
t.Errorf("RepositoryContent(%q, %q) returned unexpected error: %v", tt.encoding, tt.content, err)
}
if err == nil && tt.wantErr {
t.Errorf("RepositoryContent(%q, %q) did not return unexpected error", tt.encoding, tt.content)
}
if want := tt.want; got != want {
t.Errorf("RepositoryContent.GetContent returned %+v, want %+v", got, want)
}
}
}


Loading…
Cancel
Save