test(internal/) add test (#1331)

pull/1332/head
yuemoxi 3 years ago committed by GitHub
parent 86621a9573
commit 4b6ab21ae5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 70
      internal/endpoint/endpoint_test.go
  2. 89
      internal/httputil/http_test.go

@ -40,3 +40,73 @@ func TestEndPoint(t *testing.T) {
})
}
}
func TestNewEndpoint(t *testing.T) {
type args struct {
scheme string
host string
isSecure bool
}
tests := []struct {
name string
args args
want *url.URL
}{
{
name: "https://github.com/go-kratos/kratos/",
args: args{"https", "github.com/go-kratos/kratos/", false},
want: &url.URL{Scheme: "https", Host: "github.com/go-kratos/kratos/"},
},
{
name: "https://go-kratos.dev/",
args: args{"https", "go-kratos.dev/", true},
want: &url.URL{Scheme: "https", Host: "go-kratos.dev/", RawQuery: "isSecure=true"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewEndpoint(tt.args.scheme, tt.args.host, tt.args.isSecure); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewEndpoint() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseEndpoint(t *testing.T) {
type args struct {
endpoints []string
scheme string
isSecure bool
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "kratos",
args: args{endpoints: []string{"https://github.com/go-kratos/kratos?isSecure=true"}, scheme: "https", isSecure: true},
want: "github.com",
wantErr: false,
},
{
name: "test",
args: args{endpoints: []string{"https://go-kratos.dev/"}, scheme: "http", isSecure: true},
want: "",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseEndpoint(tt.args.endpoints, tt.args.scheme, tt.args.isSecure)
if (err != nil) != tt.wantErr {
t.Errorf("ParseEndpoint() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseEndpoint() got = %v, want %v", got, tt.want)
}
})
}
}

@ -1,6 +1,10 @@
package httputil
import "testing"
import (
"google.golang.org/grpc/codes"
"net/http"
"testing"
)
func TestContentSubtype(t *testing.T) {
tests := []struct {
@ -25,3 +29,86 @@ func TestContentSubtype(t *testing.T) {
})
}
}
func TestGRPCCodeFromStatus(t *testing.T) {
tests := []struct {
name string
code int
want codes.Code
}{
{"http.StatusOK", http.StatusOK, codes.OK},
{"http.StatusBadRequest", http.StatusBadRequest, codes.InvalidArgument},
{"http.StatusUnauthorized", http.StatusUnauthorized, codes.Unauthenticated},
{"http.StatusForbidden", http.StatusForbidden, codes.PermissionDenied},
{"http.StatusNotFound", http.StatusNotFound, codes.NotFound},
{"http.StatusConflict", http.StatusConflict, codes.Aborted},
{"http.StatusTooManyRequests", http.StatusTooManyRequests, codes.ResourceExhausted},
{"http.StatusInternalServerError", http.StatusInternalServerError, codes.Internal},
{"http.StatusNotImplemented", http.StatusNotImplemented, codes.Unimplemented},
{"http.StatusServiceUnavailable", http.StatusServiceUnavailable, codes.Unavailable},
{"http.StatusGatewayTimeout", http.StatusGatewayTimeout, codes.DeadlineExceeded},
{"StatusClientClosed", StatusClientClosed, codes.Canceled},
{"else", 100000, codes.Unknown},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GRPCCodeFromStatus(tt.code); got != tt.want {
t.Errorf("GRPCCodeFromStatus() = %v, want %v", got, tt.want)
}
})
}
}
func TestStatusFromGRPCCode(t *testing.T) {
tests := []struct {
name string
code codes.Code
want int
}{
{"codes.OK", codes.OK, http.StatusOK},
{"codes.Canceled", codes.Canceled, StatusClientClosed},
{"codes.Unknown", codes.Unknown, http.StatusInternalServerError},
{"codes.InvalidArgument", codes.InvalidArgument, http.StatusBadRequest},
{"codes.DeadlineExceeded", codes.DeadlineExceeded, http.StatusGatewayTimeout},
{"codes.NotFound", codes.NotFound, http.StatusNotFound},
{"codes.AlreadyExists", codes.AlreadyExists, http.StatusConflict},
{"codes.PermissionDenied", codes.PermissionDenied, http.StatusForbidden},
{"codes.Unauthenticated", codes.Unauthenticated, http.StatusUnauthorized},
{"codes.ResourceExhausted", codes.ResourceExhausted, http.StatusTooManyRequests},
{"codes.FailedPrecondition", codes.FailedPrecondition, http.StatusBadRequest},
{"codes.Aborted", codes.Aborted, http.StatusConflict},
{"codes.OutOfRange", codes.OutOfRange, http.StatusBadRequest},
{"codes.Unimplemented", codes.Unimplemented, http.StatusNotImplemented},
{"codes.Internal", codes.Internal, http.StatusInternalServerError},
{"codes.Unavailable", codes.Unavailable, http.StatusServiceUnavailable},
{"codes.DataLoss", codes.DataLoss, http.StatusInternalServerError},
{"else", codes.Code(10000), http.StatusInternalServerError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StatusFromGRPCCode(tt.code); got != tt.want {
t.Errorf("StatusFromGRPCCode() = %v, want %v", got, tt.want)
}
})
}
}
func TestContentType(t *testing.T) {
tests := []struct {
name string
subtype string
want string
}{
{"kratos","kratos","application/kratos"},
{"json","json","application/json"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ContentType(tt.subtype); got != tt.want {
t.Errorf("ContentType() = %v, want %v", got, tt.want)
}
})
}
}
Loading…
Cancel
Save