github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/provider_test.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package componentprotocol
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"net/http"
    21  	"strconv"
    22  	"testing"
    23  
    24  	"google.golang.org/protobuf/types/known/structpb"
    25  
    26  	"github.com/erda-project/erda-infra/providers/component-protocol/protobuf/proto-go/cp/pb"
    27  )
    28  
    29  type MockResponseWriter struct {
    30  	Head http.Header
    31  	Data bytes.Buffer
    32  }
    33  
    34  func (m *MockResponseWriter) Header() http.Header {
    35  	return m.Head
    36  }
    37  
    38  func (m *MockResponseWriter) Write(data []byte) (int, error) {
    39  	return m.Data.Write(data)
    40  }
    41  
    42  func (m *MockResponseWriter) WriteHeader(statusCode int) {
    43  	m.Head["test"] = []string{
    44  		strconv.FormatInt(int64(statusCode), 10),
    45  	}
    46  	return
    47  }
    48  
    49  type mockStr struct {
    50  }
    51  
    52  func TestEncoder(t *testing.T) {
    53  	rw := &MockResponseWriter{
    54  		Head: map[string][]string{},
    55  		Data: bytes.Buffer{},
    56  	}
    57  	err := encoder(rw, nil, nil)
    58  	if err != nil {
    59  		t.Error(err)
    60  	}
    61  
    62  	err = encoder(rw, nil, mockStr{})
    63  	if err != nil {
    64  		t.Error(err)
    65  	}
    66  
    67  	listValue, _ := structpb.NewList([]interface{}{"testUserID"})
    68  	resp := &pb.RenderResponse{
    69  		Scenario: &pb.Scenario{
    70  			ScenarioKey:  "testScenario",
    71  			ScenarioType: "testScenario",
    72  		},
    73  		Protocol: &pb.ComponentProtocol{
    74  			Version:  "0.1",
    75  			Scenario: "testScenario",
    76  			GlobalState: map[string]*structpb.Value{
    77  				"_userIDs_": structpb.NewListValue(listValue),
    78  			},
    79  			Hierarchy: &pb.Hierarchy{
    80  				Root: "page",
    81  			},
    82  			Components: map[string]*pb.Component{
    83  				"testComponent": {
    84  					Type: "container",
    85  					Name: "test",
    86  				},
    87  			},
    88  		},
    89  	}
    90  	if err = encoder(rw, nil, resp); err != nil {
    91  		t.Error(err)
    92  	}
    93  }
    94  
    95  type MockError struct {
    96  }
    97  
    98  func (e *MockError) HTTPStatus() int {
    99  	return 500
   100  }
   101  
   102  func (e *MockError) Error() string {
   103  	return ""
   104  }
   105  
   106  func TestErrorEncoder(t *testing.T) {
   107  	rw := &MockResponseWriter{
   108  		Head: map[string][]string{},
   109  		Data: bytes.Buffer{},
   110  	}
   111  	errorEncoder(rw, nil, &MockError{})
   112  	if rw.Head["test"][0] != "500" {
   113  		t.Errorf("test failed, expect head is 500, got %s", rw.Head["test"][0])
   114  	}
   115  	errorEncoder(rw, nil, errors.New("test"))
   116  }