2015-03-29 13:51:08 +00:00
|
|
|
package requestdecorator
|
|
|
|
|
|
|
|
import (
|
2015-03-30 22:27:46 +00:00
|
|
|
"encoding/base64"
|
2015-03-29 13:51:08 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-03-30 22:27:46 +00:00
|
|
|
// The following 2 functions are here for 1.3.3 support
|
|
|
|
// After we drop 1.3.3 support we can use the functions supported
|
|
|
|
// in go v1.4.0 +
|
|
|
|
// BasicAuth returns the username and password provided in the request's
|
|
|
|
// Authorization header, if the request uses HTTP Basic Authentication.
|
|
|
|
// See RFC 2617, Section 2.
|
|
|
|
func basicAuth(r *http.Request) (username, password string, ok bool) {
|
|
|
|
auth := r.Header.Get("Authorization")
|
|
|
|
if auth == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return parseBasicAuth(auth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseBasicAuth parses an HTTP Basic Authentication string.
|
|
|
|
// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
|
|
|
|
func parseBasicAuth(auth string) (username, password string, ok bool) {
|
|
|
|
const prefix = "Basic "
|
|
|
|
if !strings.HasPrefix(auth, prefix) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cs := string(c)
|
|
|
|
s := strings.IndexByte(cs, ':')
|
|
|
|
if s < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return cs[:s], cs[s+1:], true
|
|
|
|
}
|
|
|
|
|
2015-03-29 13:51:08 +00:00
|
|
|
func TestUAVersionInfo(t *testing.T) {
|
|
|
|
uavi := NewUAVersionInfo("foo", "bar")
|
|
|
|
if !uavi.isValid() {
|
|
|
|
t.Fatalf("UAVersionInfo should be valid")
|
|
|
|
}
|
|
|
|
uavi = NewUAVersionInfo("", "bar")
|
|
|
|
if uavi.isValid() {
|
|
|
|
t.Fatalf("Expected UAVersionInfo to be invalid")
|
|
|
|
}
|
|
|
|
uavi = NewUAVersionInfo("foo", "")
|
|
|
|
if uavi.isValid() {
|
|
|
|
t.Fatalf("Expected UAVersionInfo to be invalid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserAgentDecorator(t *testing.T) {
|
|
|
|
httpVersion := make([]UAVersionInfo, 2)
|
|
|
|
httpVersion = append(httpVersion, NewUAVersionInfo("testname", "testversion"))
|
|
|
|
httpVersion = append(httpVersion, NewUAVersionInfo("name", "version"))
|
|
|
|
uad := &UserAgentDecorator{
|
|
|
|
Versions: httpVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/something", strings.NewReader("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
reqDecorated, err := uad.ChangeRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqDecorated.Header.Get("User-Agent") != "testname/testversion name/version" {
|
|
|
|
t.Fatalf("Request should have User-Agent 'testname/testversion name/version'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserAgentDecoratorErr(t *testing.T) {
|
|
|
|
httpVersion := make([]UAVersionInfo, 0)
|
|
|
|
uad := &UserAgentDecorator{
|
|
|
|
Versions: httpVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
var req *http.Request
|
|
|
|
_, err := uad.ChangeRequest(req)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected to get ErrNilRequest instead no error was returned")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaHeadersDecorator(t *testing.T) {
|
|
|
|
var headers = map[string][]string{
|
|
|
|
"key1": {"value1"},
|
|
|
|
"key2": {"value2"},
|
|
|
|
}
|
|
|
|
mhd := &MetaHeadersDecorator{
|
|
|
|
Headers: headers,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/something", strings.NewReader("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
reqDecorated, err := mhd.ChangeRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := reqDecorated.Header["key1"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected to have header key1")
|
|
|
|
}
|
|
|
|
if v[0] != "value1" {
|
|
|
|
t.Fatalf("Expected value for key1 isn't value1")
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok = reqDecorated.Header["key2"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected to have header key2")
|
|
|
|
}
|
|
|
|
if v[0] != "value2" {
|
|
|
|
t.Fatalf("Expected value for key2 isn't value2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaHeadersDecoratorErr(t *testing.T) {
|
|
|
|
mhd := &MetaHeadersDecorator{}
|
|
|
|
|
|
|
|
var req *http.Request
|
|
|
|
_, err := mhd.ChangeRequest(req)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected to get ErrNilRequest instead no error was returned")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthDecorator(t *testing.T) {
|
|
|
|
ad := NewAuthDecorator("test", "password")
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/something", strings.NewReader("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
reqDecorated, err := ad.ChangeRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-30 22:27:46 +00:00
|
|
|
username, password, ok := basicAuth(reqDecorated)
|
2015-03-29 13:51:08 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
|
|
}
|
|
|
|
if username != "test" {
|
|
|
|
t.Fatalf("Expected username to be test, got %s", username)
|
|
|
|
}
|
|
|
|
if password != "password" {
|
|
|
|
t.Fatalf("Expected password to be password, got %s", password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthDecoratorErr(t *testing.T) {
|
|
|
|
ad := &AuthDecorator{}
|
|
|
|
|
|
|
|
var req *http.Request
|
|
|
|
_, err := ad.ChangeRequest(req)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected to get ErrNilRequest instead no error was returned")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestFactory(t *testing.T) {
|
|
|
|
ad := NewAuthDecorator("test", "password")
|
|
|
|
httpVersion := make([]UAVersionInfo, 2)
|
|
|
|
httpVersion = append(httpVersion, NewUAVersionInfo("testname", "testversion"))
|
|
|
|
httpVersion = append(httpVersion, NewUAVersionInfo("name", "version"))
|
|
|
|
uad := &UserAgentDecorator{
|
|
|
|
Versions: httpVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
requestFactory := NewRequestFactory(ad, uad)
|
|
|
|
|
2015-04-03 04:03:30 +00:00
|
|
|
if dlen := len(requestFactory.GetDecorators()); dlen != 2 {
|
2015-03-29 13:51:08 +00:00
|
|
|
t.Fatalf("Expected to have two decorators, got %d", dlen)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := requestFactory.NewRequest("GET", "/test", strings.NewReader("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-30 22:27:46 +00:00
|
|
|
username, password, ok := basicAuth(req)
|
2015-03-29 13:51:08 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
|
|
}
|
|
|
|
if username != "test" {
|
|
|
|
t.Fatalf("Expected username to be test, got %s", username)
|
|
|
|
}
|
|
|
|
if password != "password" {
|
|
|
|
t.Fatalf("Expected password to be password, got %s", password)
|
|
|
|
}
|
|
|
|
if req.Header.Get("User-Agent") != "testname/testversion name/version" {
|
|
|
|
t.Fatalf("Request should have User-Agent 'testname/testversion name/version'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestFactoryNewRequestWithDecorators(t *testing.T) {
|
|
|
|
ad := NewAuthDecorator("test", "password")
|
|
|
|
|
|
|
|
requestFactory := NewRequestFactory(ad)
|
|
|
|
|
2015-04-03 04:03:30 +00:00
|
|
|
if dlen := len(requestFactory.GetDecorators()); dlen != 1 {
|
2015-03-29 13:51:08 +00:00
|
|
|
t.Fatalf("Expected to have one decorators, got %d", dlen)
|
|
|
|
}
|
|
|
|
|
|
|
|
ad2 := NewAuthDecorator("test2", "password2")
|
|
|
|
|
|
|
|
req, err := requestFactory.NewRequest("GET", "/test", strings.NewReader("test"), ad2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-30 22:27:46 +00:00
|
|
|
username, password, ok := basicAuth(req)
|
2015-03-29 13:51:08 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Cannot retrieve basic auth info from request")
|
|
|
|
}
|
|
|
|
if username != "test2" {
|
|
|
|
t.Fatalf("Expected username to be test, got %s", username)
|
|
|
|
}
|
|
|
|
if password != "password2" {
|
|
|
|
t.Fatalf("Expected password to be password, got %s", password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestFactoryAddDecorator(t *testing.T) {
|
|
|
|
requestFactory := NewRequestFactory()
|
|
|
|
|
2015-04-03 04:03:30 +00:00
|
|
|
if dlen := len(requestFactory.GetDecorators()); dlen != 0 {
|
2015-03-29 13:51:08 +00:00
|
|
|
t.Fatalf("Expected to have zero decorators, got %d", dlen)
|
|
|
|
}
|
|
|
|
|
|
|
|
ad := NewAuthDecorator("test", "password")
|
|
|
|
requestFactory.AddDecorator(ad)
|
|
|
|
|
2015-04-03 04:03:30 +00:00
|
|
|
if dlen := len(requestFactory.GetDecorators()); dlen != 1 {
|
2015-03-29 13:51:08 +00:00
|
|
|
t.Fatalf("Expected to have one decorators, got %d", dlen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestFactoryNil(t *testing.T) {
|
|
|
|
var requestFactory RequestFactory
|
|
|
|
_, err := requestFactory.NewRequest("GET", "/test", strings.NewReader("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected not to get and error, got %s", err)
|
|
|
|
}
|
|
|
|
}
|