File: basic_test.go

package info (click to toggle)
golang-github-abbot-go-http-auth 0.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 164 kB
  • sloc: makefile: 13
file content (40 lines) | stat: -rw-r--r-- 989 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package auth

import (
	"encoding/base64"
	"net/http"
	"testing"
)

func TestAuthBasic(t *testing.T) {
	secrets := HtpasswdFileProvider("test.htpasswd")
	a := &BasicAuth{Realm: "example.com", Secrets: secrets}
	r := &http.Request{}
	r.Method = "GET"
	if a.CheckAuth(r) != "" {
		t.Fatal("CheckAuth passed on empty headers")
	}
	r.Header = http.Header(make(map[string][]string))
	r.Header.Set("Authorization", "Digest blabla ololo")
	if a.CheckAuth(r) != "" {
		t.Fatal("CheckAuth passed on bad headers")
	}
	r.Header.Set("Authorization", "Basic !@#")
	if a.CheckAuth(r) != "" {
		t.Fatal("CheckAuth passed on bad base64 data")
	}

	data := [][]string{
		{"test", "hello"},
		{"test2", "hello2"},
		{"test3", "hello3"},
		{"test16", "topsecret"},
	}
	for _, tc := range data {
		auth := base64.StdEncoding.EncodeToString([]byte(tc[0] + ":" + tc[1]))
		r.Header.Set("Authorization", "Basic "+auth)
		if a.CheckAuth(r) != tc[0] {
			t.Fatalf("CheckAuth failed for user '%s'", tc[0])
		}
	}
}