Skip to content

Commit

Permalink
Omit an empty value for user data to serialize properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryce Reitano committed Apr 13, 2015
1 parent 006aa44 commit 107613d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type DropletCreateRequest struct {
Backups bool `json:"backups"`
IPv6 bool `json:"ipv6"`
PrivateNetworking bool `json:"private_networking"`
UserData string `json:"user_data"`
UserData string `json:"user_data,omitempty"`
}

func (d DropletCreateRequest) String() string {
Expand Down
5 changes: 2 additions & 3 deletions droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func TestDroplets_ListDropletsMultiplePages(t *testing.T) {

dr := dropletsRoot{
Droplets: []Droplet{
Droplet{ID: 1},
Droplet{ID: 2},
{ID: 1},
{ID: 2},
},
Links: &Links{
Pages: &Pages{Next: "http://example.com/v2/droplets/?page=2"},
Expand Down Expand Up @@ -135,7 +135,6 @@ func TestDroplets_Create(t *testing.T) {
"backups": false,
"ipv6": false,
"private_networking": false,
"user_data": "",
}

var v map[string]interface{}
Expand Down
30 changes: 29 additions & 1 deletion godo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,35 @@ func TestNewRequest(t *testing.T) {
inBody, outBody := &DropletCreateRequest{Name: "l"},
`{"name":"l","region":"","size":"","image":0,`+
`"ssh_keys":null,"backups":false,"ipv6":false,`+
`"private_networking":false,"user_data":""}`+"\n"
`"private_networking":false}`+"\n"
req, _ := c.NewRequest("GET", inURL, inBody)

// test relative URL was expanded
if req.URL.String() != outURL {
t.Errorf("NewRequest(%v) URL = %v, expected %v", inURL, req.URL, outURL)
}

// test body was JSON encoded
body, _ := ioutil.ReadAll(req.Body)
if string(body) != outBody {
t.Errorf("NewRequest(%v)Body = %v, expected %v", inBody, string(body), outBody)
}

// test default user-agent is attached to the request
userAgent := req.Header.Get("User-Agent")
if c.UserAgent != userAgent {
t.Errorf("NewRequest() User-Agent = %v, expected %v", userAgent, c.UserAgent)
}
}

func TestNewRequest_withUserData(t *testing.T) {
c := NewClient(nil)

inURL, outURL := "/foo", defaultBaseURL+"foo"
inBody, outBody := &DropletCreateRequest{Name: "l", UserData: "u"},
`{"name":"l","region":"","size":"","image":0,`+
`"ssh_keys":null,"backups":false,"ipv6":false,`+
`"private_networking":false,"user_data":"u"}`+"\n"
req, _ := c.NewRequest("GET", inURL, inBody)

// test relative URL was expanded
Expand Down

0 comments on commit 107613d

Please sign in to comment.