github.com/oam-dev/kubevela@v1.9.11/pkg/cue/task/process_test.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package task
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net"
    23  	"net/http"
    24  	"net/http/httptest"
    25  	"testing"
    26  
    27  	"github.com/kubevela/workflow/pkg/cue/model/value"
    28  
    29  	"cuelang.org/go/cue/cuecontext"
    30  	cueJson "cuelang.org/go/pkg/encoding/json"
    31  	"github.com/stretchr/testify/assert"
    32  
    33  	"github.com/oam-dev/kubevela/pkg/cue/process"
    34  )
    35  
    36  const TaskTemplate = `
    37  parameter: {
    38    serviceURL: string
    39  }
    40  
    41  processing: {
    42    output: {
    43      token ?: string
    44    }
    45    http: {
    46      method: *"GET" | string
    47      url: parameter.serviceURL
    48      request: {
    49          body ?: bytes
    50          header: {}
    51          trailer: {}
    52      }
    53    }
    54  }
    55  
    56  patch: {
    57    data: token: processing.output.token
    58  }
    59  
    60  output: {
    61    data: processing.output.token
    62  }
    63  `
    64  
    65  func TestProcess(t *testing.T) {
    66  	s := NewMock()
    67  	defer s.Close()
    68  
    69  	taskTemplate := cuecontext.New().CompileString(TaskTemplate)
    70  	taskTemplate = taskTemplate.FillPath(value.FieldPath(process.ParameterFieldName), map[string]interface{}{
    71  		"serviceURL": "http://127.0.0.1:8090/api/v1/token?val=test-token",
    72  	})
    73  
    74  	inst, err := Process(taskTemplate)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	output := inst.LookupPath(value.FieldPath("output"))
    79  	data, _ := cueJson.Marshal(output)
    80  	assert.Equal(t, "{\"data\":\"test-token\"}", data)
    81  }
    82  
    83  func NewMock() *httptest.Server {
    84  	ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    85  		if r.Method != "GET" {
    86  			fmt.Printf("Expected 'GET' request, got '%s'", r.Method)
    87  		}
    88  		if r.URL.EscapedPath() != "/api/v1/token" {
    89  			fmt.Printf("Expected request to '/person', got '%s'", r.URL.EscapedPath())
    90  		}
    91  		_ = r.ParseForm()
    92  		token := r.Form.Get("val")
    93  		tokenBytes, _ := json.Marshal(map[string]interface{}{"token": token})
    94  
    95  		w.WriteHeader(http.StatusOK)
    96  		w.Write(tokenBytes)
    97  	}))
    98  	l, _ := net.Listen("tcp", "127.0.0.1:8090")
    99  	_ = ts.Listener.Close()
   100  	ts.Listener = l
   101  	ts.Start()
   102  	return ts
   103  }