๐Ÿ“ฆ go-chi / oauth

๐Ÿ“„ basic_test.go ยท 54 lines
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54package oauth

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

func TestGetBasicAuthentication(t *testing.T) {
	req, _ := http.NewRequest("GET", "/token", nil)
	req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password123456")))

	username, password, err := GetBasicAuthentication(req)
	if err != nil {
		t.Fatalf("Error %s", err.Error())
	} else {
		if username != "admin" {
			t.Fatalf("Wrong Username = %s", username)
		}
		if password != "password123456" {
			t.Fatalf("Wrong Username = %s", password)
		}
	}
}

func TestVoidBasicAuthentication(t *testing.T) {
	req, _ := http.NewRequest("GET", "/token", nil)

	username, password, err := GetBasicAuthentication(req)
	if err != nil {
		t.Fatalf("Error %s", err.Error())
	} else {
		if username != "" {
			t.Fatalf("Wrong Username = %s", username)
		}
		if password != "" {
			t.Fatalf("Wrong Username = %s", password)
		}
	}

}

func TestCheckBasicAuthentication(t *testing.T) {
	req, _ := http.NewRequest("GET", "/token", nil)
	req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password123456")))

	err := CheckBasicAuthentication("admin", "password123456", req)
	if err != nil {
		t.Fatalf("Error %s", err.Error())
	} else {
		t.Log("Credentials are OK")
	}
}