github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/transport/http/handle_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 http
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/erda-project/erda-infra/pkg/transport/interceptor"
    24  )
    25  
    26  type testInterceptor struct {
    27  	key    string
    28  	append func(v string)
    29  }
    30  
    31  func (i testInterceptor) WrapHTTP(h http.HandlerFunc) http.HandlerFunc {
    32  	return func(rw http.ResponseWriter, r *http.Request) {
    33  		i.append(i.key + "->")
    34  		h(rw, r)
    35  		i.append("<-" + i.key)
    36  	}
    37  }
    38  
    39  func (i testInterceptor) Wrap(h interceptor.Handler) interceptor.Handler {
    40  	return func(ctx context.Context, req interface{}) (interface{}, error) {
    41  		i.append(i.key + "->")
    42  		h(ctx, req)
    43  		i.append("<-" + i.key)
    44  		return nil, nil
    45  	}
    46  }
    47  
    48  func TestWithHTTPInterceptor(t *testing.T) {
    49  	tests := []struct {
    50  		name   string
    51  		handle string
    52  		inters []*testInterceptor
    53  		want   []string
    54  	}{
    55  		{
    56  			handle: "handle",
    57  			inters: []*testInterceptor{
    58  				{key: "a"},
    59  				{key: "b"},
    60  				{key: "c"},
    61  			},
    62  			want: []string{"a->", "b->", "c->", "handle", "<-c", "<-b", "<-a"},
    63  		},
    64  		{
    65  			handle: "handle",
    66  			want:   []string{"handle"},
    67  		},
    68  		{
    69  			handle: "handle",
    70  			inters: []*testInterceptor{
    71  				{key: "a"},
    72  			},
    73  			want: []string{"a->", "handle", "<-a"},
    74  		},
    75  		{
    76  			handle: "handle",
    77  			inters: []*testInterceptor{
    78  				{key: "a"},
    79  				{key: "b"},
    80  			},
    81  			want: []string{"a->", "b->", "handle", "<-b", "<-a"},
    82  		},
    83  	}
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			var results []string
    87  			add := func(v string) {
    88  				results = append(results, v)
    89  			}
    90  			handler := func(rw http.ResponseWriter, r *http.Request) {
    91  				add(tt.handle)
    92  			}
    93  			opts := DefaultHandleOptions()
    94  			for _, i := range tt.inters {
    95  				i.append = add
    96  				WithHTTPInterceptor(i.WrapHTTP)(opts)
    97  			}
    98  			if opts.HTTPInterceptor != nil {
    99  				handler = opts.HTTPInterceptor(handler)
   100  			}
   101  			handler(nil, nil)
   102  			if !reflect.DeepEqual(results, tt.want) {
   103  				t.Errorf("wrapped http handler got %v, want %v", results, tt.want)
   104  			}
   105  		})
   106  	}
   107  }
   108  
   109  func TestWithInterceptor(t *testing.T) {
   110  	tests := []struct {
   111  		name   string
   112  		handle string
   113  		inters []*testInterceptor
   114  		want   []string
   115  	}{
   116  		{
   117  			handle: "handle",
   118  			inters: []*testInterceptor{
   119  				{key: "a"},
   120  				{key: "b"},
   121  				{key: "c"},
   122  			},
   123  			want: []string{"a->", "b->", "c->", "handle", "<-c", "<-b", "<-a"},
   124  		},
   125  		{
   126  			handle: "handle",
   127  			want:   []string{"handle"},
   128  		},
   129  		{
   130  			handle: "handle",
   131  			inters: []*testInterceptor{
   132  				{key: "a"},
   133  			},
   134  			want: []string{"a->", "handle", "<-a"},
   135  		},
   136  		{
   137  			handle: "handle",
   138  			inters: []*testInterceptor{
   139  				{key: "a"},
   140  				{key: "b"},
   141  			},
   142  			want: []string{"a->", "b->", "handle", "<-b", "<-a"},
   143  		},
   144  	}
   145  	for _, tt := range tests {
   146  		t.Run(tt.name, func(t *testing.T) {
   147  			var results []string
   148  			add := func(v string) {
   149  				results = append(results, v)
   150  			}
   151  			handler := func(ctx context.Context, req interface{}) (interface{}, error) {
   152  				add(tt.handle)
   153  				return nil, nil
   154  			}
   155  			opts := DefaultHandleOptions()
   156  			for _, i := range tt.inters {
   157  				i.append = add
   158  				WithInterceptor(i.Wrap)(opts)
   159  			}
   160  			if opts.Interceptor != nil {
   161  				handler = opts.Interceptor(handler)
   162  			}
   163  			handler(nil, nil)
   164  			if !reflect.DeepEqual(results, tt.want) {
   165  				t.Errorf("wrapped handler got %v, want %v", results, tt.want)
   166  			}
   167  		})
   168  	}
   169  }