mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-23 22:46:53 +00:00
* provider http * method * add license header * test cases * test cases * test cases * test cases * generate * lint * upgrade apply component * cover * test install
121 lines
2.6 KiB
Go
121 lines
2.6 KiB
Go
/*
|
|
Copyright 2021 The KubeVela Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package http
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"cuelang.org/go/cue"
|
|
"github.com/bmizerany/assert"
|
|
|
|
"github.com/oam-dev/kubevela/pkg/builtin/registry"
|
|
)
|
|
|
|
const (
|
|
Req = `
|
|
{
|
|
method: *"GET" | string
|
|
url: "http://127.0.0.1:8090/api/v1/token?val=test-token"
|
|
request: {
|
|
body ?: bytes
|
|
header: {
|
|
"Accept-Language": "en,nl"
|
|
}
|
|
trailer: {
|
|
"Accept-Language": "en,nl"
|
|
User: "foo"
|
|
}
|
|
}
|
|
}
|
|
`
|
|
ReqWithoutHeader = `
|
|
{
|
|
method: *"GET" | string
|
|
url: "http://127.0.0.1:8090/api/v1/token?val=test-token-no-header"
|
|
request: {
|
|
body ?: bytes
|
|
trailer: {
|
|
"Accept-Language": "en,nl"
|
|
User: "foo"
|
|
}
|
|
}
|
|
}
|
|
`
|
|
)
|
|
|
|
func TestHTTPCmdRun(t *testing.T) {
|
|
s := NewMock()
|
|
defer s.Close()
|
|
|
|
r := cue.Runtime{}
|
|
reqInst, err := r.Compile("", Req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
runner, _ := newHTTPCmd(cue.Value{})
|
|
got, err := runner.Run(®istry.Meta{Obj: reqInst.Value()})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
body := (got.(map[string]interface{}))["body"].(string)
|
|
|
|
assert.Equal(t, "{\"token\":\"test-token\"}", body)
|
|
|
|
reqNoHeaderInst, err := r.Compile("", ReqWithoutHeader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, err = runner.Run(®istry.Meta{Obj: reqNoHeaderInst.Value()})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
body = (got.(map[string]interface{}))["body"].(string)
|
|
|
|
assert.Equal(t, "{\"token\":\"test-token-no-header\"}", body)
|
|
|
|
}
|
|
|
|
// NewMock mock the http server
|
|
func NewMock() *httptest.Server {
|
|
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
fmt.Printf("Expected 'GET' request, got '%s'", r.Method)
|
|
}
|
|
if r.URL.EscapedPath() != "/api/v1/token" {
|
|
fmt.Printf("Expected request to '/person', got '%s'", r.URL.EscapedPath())
|
|
}
|
|
r.ParseForm()
|
|
token := r.Form.Get("val")
|
|
tokenBytes, _ := json.Marshal(map[string]interface{}{"token": token})
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write(tokenBytes)
|
|
}))
|
|
l, _ := net.Listen("tcp", "127.0.0.1:8090")
|
|
ts.Listener.Close()
|
|
ts.Listener = l
|
|
ts.Start()
|
|
return ts
|
|
}
|