diff --git a/http/headers/headers.go b/http/headers/headers.go new file mode 100644 index 0000000..e1ff87f --- /dev/null +++ b/http/headers/headers.go @@ -0,0 +1,7 @@ +package headers + +// HTTP headers +const ( + ContentType = "Content-Type" + Accept = "Accept" +) diff --git a/http/mediatypes/mediatypes.go b/http/mediatypes/mediatypes.go new file mode 100644 index 0000000..7cc74e2 --- /dev/null +++ b/http/mediatypes/mediatypes.go @@ -0,0 +1,7 @@ +package mediatypes + +// Media types +const ( + ApplicationJSON = "application/json" + ApplicationJSONUtf8 = "application/json; charset=utf-8" +) diff --git a/remote.go b/remote.go index 2c2db80..ac4eb60 100644 --- a/remote.go +++ b/remote.go @@ -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" ) @@ -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 } @@ -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)