Skip to content

Commit

Permalink
Fix basic http auth
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Aug 31, 2016
1 parent a2f5e43 commit 1674b58
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 10 additions & 0 deletions http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"crypto/tls"
"encoding/base64"
"io"
"log"
"net"
Expand Down Expand Up @@ -44,6 +45,7 @@ type HTTPClient struct {
baseURL string
scheme string
host string
auth string
conn net.Conn
respBuf []byte
config *HTTPClientConfig
Expand Down Expand Up @@ -79,6 +81,10 @@ func NewHTTPClient(baseURL string, config *HTTPClientConfig) *HTTPClient {
client.respBuf = make([]byte, config.ResponseBufferSize)
client.config = config

if u.User != nil {
client.auth = "Basic " + base64.StdEncoding.EncodeToString([]byte(u.User.String()))
}

return client
}

Expand Down Expand Up @@ -164,6 +170,10 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
data = proto.SetHost(data, []byte(c.baseURL), []byte(c.host))
}

if c.auth != "" {
data = proto.SetHeader(data, []byte("Authorization"), []byte(c.auth))
}

if c.config.Debug {
Debug("[HTTPClient] Sending:", string(data))
}
Expand Down
42 changes: 41 additions & 1 deletion http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http/httptest"
"net/http/httputil"
_ "reflect"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestHTTPClientResonseByClose(t *testing.T) {

payload := []byte("GET / HTTP/1.1\r\n\r\n")
ln, _ := net.Listen("tcp", ":0")
go func(){
go func() {
for {
conn, _ := ln.Accept()
buf := make([]byte, 4096)
Expand Down Expand Up @@ -356,6 +357,45 @@ func TestHTTPClientRedirectLimit(t *testing.T) {
wg.Wait()
}

func TestHTTPClientBasicAuth(t *testing.T) {
wg := new(sync.WaitGroup)
wg.Add(2)

GETPayload := []byte("GET / HTTP/1.1\r\n\r\n")

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, _ := r.BasicAuth()

if user != "user" || pass != "pass" {
http.Error(w, "Unauthorized.", 401)
wg.Done()
return
}

wg.Done()
}))
defer server.Close()

client := NewHTTPClient(server.URL, &HTTPClientConfig{Debug: false})
resp, _ := client.Send(GETPayload)
client.Disconnect()

if !bytes.Equal(proto.Status(resp), []byte("401")) {
t.Error("Should return unauthorized error", string(resp))
}

authUrl := strings.Replace(server.URL, "http://", "http://user:pass@", -1)
client = NewHTTPClient(authUrl, &HTTPClientConfig{Debug: false})
resp, _ = client.Send(GETPayload)
client.Disconnect()

if !bytes.Equal(proto.Status(resp), []byte("200")) {
t.Error("Should return proper response", string(resp))
}

wg.Wait()
}

func TestHTTPClientHandleHTTP10(t *testing.T) {
wg := new(sync.WaitGroup)

Expand Down

0 comments on commit 1674b58

Please sign in to comment.