Send the service credentials to initial /api request

This commit is contained in:
Paul Bellamy
2015-10-16 13:08:51 +01:00
parent 5ed324071c
commit 637cb23ba8
2 changed files with 29 additions and 16 deletions

View File

@@ -23,8 +23,16 @@ var fastClient = http.Client{
// NewHTTPPublisher returns an HTTPPublisher ready for use.
func NewHTTPPublisher(target, token, probeID string) (string, *HTTPPublisher, error) {
targetAPI := sanitize.URL("http://", 0, "/api")(target)
resp, err := fastClient.Get(targetAPI)
p := &HTTPPublisher{
url: sanitize.URL("http://", 0, "/api/report")(target),
token: token,
probeID: probeID,
}
req, err := p.authorizedRequest("GET", sanitize.URL("http://", 0, "/api")(target), nil)
if err != nil {
return "", nil, err
}
resp, err := fastClient.Do(req)
if err != nil {
return "", nil, err
}
@@ -35,11 +43,16 @@ func NewHTTPPublisher(target, token, probeID string) (string, *HTTPPublisher, er
if err := json.NewDecoder(resp.Body).Decode(&apiResponse); err != nil {
return "", nil, err
}
return apiResponse.ID, &HTTPPublisher{
url: sanitize.URL("http://", 0, "/api/report")(target),
token: token,
probeID: probeID,
}, nil
return apiResponse.ID, p, nil
}
func (p HTTPPublisher) authorizedRequest(method string, urlStr string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, urlStr, body)
if err == nil {
req.Header.Set("Authorization", AuthorizationHeader(p.token))
req.Header.Set(ScopeProbeIDHeader, p.probeID)
}
return req, err
}
func (p HTTPPublisher) String() string {
@@ -48,12 +61,10 @@ func (p HTTPPublisher) String() string {
// Publish publishes the report to the URL.
func (p HTTPPublisher) Publish(r io.Reader) error {
req, err := http.NewRequest("POST", p.url, r)
req, err := p.authorizedRequest("POST", p.url, r)
if err != nil {
return err
}
req.Header.Set("Authorization", AuthorizationHeader(p.token))
req.Header.Set(ScopeProbeIDHeader, p.probeID)
req.Header.Set("Content-Encoding", "gzip")
// req.Header.Set("Content-Type", "application/binary") // TODO: we should use http.DetectContentType(..) on the gob'ed

View File

@@ -26,17 +26,19 @@ func TestHTTPPublisher(t *testing.T) {
)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if want, have := xfer.AuthorizationHeader(token), r.Header.Get("Authorization"); want != have {
t.Errorf("want %q, have %q", want, have)
}
if want, have := id, r.Header.Get(xfer.ScopeProbeIDHeader); want != have {
t.Errorf("want %q, have %q", want, have)
}
if r.URL.Path == "/api" {
_ = json.NewEncoder(w).Encode(map[string]string{"id": "irrelevant"})
return
}
if want, have := xfer.AuthorizationHeader(token), r.Header.Get("Authorization"); want != have {
t.Errorf("want %q, have %q", want, have)
}
if want, have := id, r.Header.Get(xfer.ScopeProbeIDHeader); want != have {
t.Errorf("want %q, have %q", want, have)
}
var have report.Report
reader := r.Body