Skip to content

Commit

Permalink
Set content type header when payload is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
aziemski committed Aug 18, 2020
1 parent 9a0798f commit 35b7abd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
7 changes: 7 additions & 0 deletions http/headers/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package headers

// HTTP headers
const (
ContentType = "Content-Type"
Accept = "Accept"
)
7 changes: 7 additions & 0 deletions http/mediatypes/mediatypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mediatypes

// Media types
const (
ApplicationJSON = "application/json"
ApplicationJSONUtf8 = "application/json; charset=utf-8"
)
20 changes: 11 additions & 9 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

"github.com/blang/semver"
"github.com/tebeka/selenium/firefox"
"github.com/tebeka/selenium/http/headers"
"github.com/tebeka/selenium/http/mediatypes"
"github.com/tebeka/selenium/log"
)

Expand Down Expand Up @@ -59,15 +61,15 @@ type remoteWD struct {
// server.
var HTTPClient = http.DefaultClient

// jsonContentType is JSON content type.
const jsonContentType = "application/json"

func newRequest(method string, url string, data []byte) (*http.Request, error) {
request, err := http.NewRequest(method, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
request.Header.Add("Accept", jsonContentType)
if data != nil {
request.Header.Add(headers.ContentType, mediatypes.ApplicationJSONUtf8)
}
request.Header.Add(headers.Accept, mediatypes.ApplicationJSON)

return request, nil
}
Expand Down Expand Up @@ -148,19 +150,19 @@ func executeCommand(method, url string, data []byte) (json.RawMessage, error) {
buf = prettyBuf.Bytes()
}
}
debugLog("<- %s [%s]\n%s", response.Status, response.Header["Content-Type"], buf)
debugLog("<- %s [%s]\n%s", response.Status, response.Header[headers.ContentType], buf)
}
if err != nil {
return nil, errors.New(response.Status)
}

fullCType := response.Header.Get("Content-Type")
fullCType := response.Header.Get(headers.ContentType)
cType, _, err := mime.ParseMediaType(fullCType)
if err != nil {
return nil, fmt.Errorf("got content type header %q, expected %q", fullCType, jsonContentType)
return nil, fmt.Errorf("got content type header %q, expected %q", fullCType, mediatypes.ApplicationJSON)
}
if cType != jsonContentType {
return nil, fmt.Errorf("got content type %q, expected %q", cType, jsonContentType)
if cType != mediatypes.ApplicationJSON {
return nil, fmt.Errorf("got content type %q, expected %q", cType, mediatypes.ApplicationJSON)
}

reply := new(serverReply)
Expand Down

0 comments on commit 35b7abd

Please sign in to comment.