package kong

import (
	"net/http"
	"reflect"
	"testing"
)

func MockRequest(authType, authContent string) *http.Request {
	return &http.Request{
		Header: map[string][]string{
			"X-Auth-Type":    {authType},
			"X-Auth-Content": {authContent},
		},
	}
}

func TestGetOpenAPIAuthResult(t *testing.T) {
	type args struct {
		r *http.Request
	}
	tests := []struct {
		name    string
		args    args
		want    *OpenAPIAuthResult
		wantErr bool
	}{
		{"Empty Auth Type", args{MockRequest("", "")}, nil, true},
		{"Unknown Auth Type", args{MockRequest("xxx", "")}, nil, true},
		{"Empty Auth Content", args{MockRequest("openapi", "")}, nil, true},
		{"Unknown Auth Content", args{MockRequest("openapi", "xxx")}, nil, true},
		{"Empty Auth Content", args{MockRequest("openapi", "{}")}, nil, true},
		{"Normal Auth Content", args{MockRequest("openapi", "{\"public_key\":\"test\",\"cidrs\":[\"0.0.0.0/0\"]}")}, &OpenAPIAuthResult{PublicKey: "test", CIDRs: []string{"0.0.0.0/0"}}, false},
		{"No CIDR Auth Content", args{MockRequest("openapi", "{\"public_key\":\"test\"}")}, &OpenAPIAuthResult{PublicKey: "test"}, false},
		{"No Public Key Auth Content", args{MockRequest("openapi", "{\"cidrs\":[\"0.0.0.0/0\"]}")}, nil, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := GetOpenAPIAuthResult(tt.args.r)
			if (err != nil) != tt.wantErr {
				t.Errorf("GetOpenAPIAuthResult() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("GetOpenAPIAuthResult() = %v, want %v", got, tt.want)
			}
		})
	}
}
