Beispiel #1
0
func TestBuildFullURL(t *testing.T) {
	for _, p := range bfuOk {
		actual, err := util.BuildFullURL(p.host, p.path, p.query)

		if err != nil {
			t.Errorf("expected %v, got an error %v", p.expected, err)
		} else if _, ok := p.expected[actual]; !ok {
			t.Errorf("expected %v, got %v", p.expected, actual)
		}
	}

	for _, p := range bfuErr {
		actual, err := util.BuildFullURL(p.host, p.path, p.query)

		if actual != "" {
			t.Errorf("expected an error %v, got a value %v", p.expected, actual)
		} else if reflect.TypeOf(err) != p.expected {
			t.Errorf("expected an error %v, got an error %v", p.expected, err)
		}
	}
}
Beispiel #2
0
// does the actual legwork, going to Nexus and validating the response.
func (nexus Nexus2x) fetch(path string, query map[string]string) (*http.Response, error) {
	fullURL, err := util.BuildFullURL(nexus.URL, path, query)
	if err != nil {
		return nil, err
	}

	get, err := http.NewRequest("GET", fullURL, nil)
	if err != nil {
		return nil, err
	}

	nexus.Credentials.Sign(get)

	// by default Nexus returns XML, but it's cheap to be explicit
	get.Header.Add("Accept", "application/xml")

	// go for it!
	response, err := nexus.HTTPClient.Do(get)
	if err != nil {
		return nil, err
	}

	// lets see if everything is alright
	status := response.StatusCode
	switch {
	case status == http.StatusUnauthorized:
		// the credentials don't check out
		return nil, &credentials.Error{URL: fullURL, Credentials: nexus.Credentials}
	case 400 <= status && status < 600:
		// Nexus complained, so error out
		return nil, nexus.errorFromResponse(response)
	}

	// all is good, carry on
	return response, nil
}