github.com/cloudwego/hertz@v0.9.3/pkg/common/config/request_option_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo 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 config 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/cloudwego/hertz/pkg/common/test/assert" 24 ) 25 26 // TestRequestOptions test request options with custom values 27 func TestRequestOptions(t *testing.T) { 28 opt := NewRequestOptions([]RequestOption{ 29 WithTag("a", "b"), 30 WithTag("c", "d"), 31 WithTag("e", "f"), 32 WithSD(true), 33 WithDialTimeout(time.Second), 34 WithReadTimeout(time.Second), 35 WithWriteTimeout(time.Second), 36 }) 37 assert.DeepEqual(t, "b", opt.Tag("a")) 38 assert.DeepEqual(t, "d", opt.Tag("c")) 39 assert.DeepEqual(t, "f", opt.Tag("e")) 40 assert.DeepEqual(t, time.Second, opt.DialTimeout()) 41 assert.DeepEqual(t, time.Second, opt.ReadTimeout()) 42 assert.DeepEqual(t, time.Second, opt.WriteTimeout()) 43 assert.True(t, opt.IsSD()) 44 } 45 46 // TestRequestOptionsWithDefaultOpts test request options with default values 47 func TestRequestOptionsWithDefaultOpts(t *testing.T) { 48 SetPreDefinedOpts(WithTag("pre-defined", "blablabla"), WithTag("a", "default-value"), WithSD(true)) 49 opt := NewRequestOptions([]RequestOption{ 50 WithTag("a", "b"), 51 WithSD(false), 52 }) 53 assert.DeepEqual(t, "b", opt.Tag("a")) 54 assert.DeepEqual(t, "blablabla", opt.Tag("pre-defined")) 55 assert.DeepEqual(t, map[string]string{ 56 "a": "b", 57 "pre-defined": "blablabla", 58 }, opt.Tags()) 59 assert.False(t, opt.IsSD()) 60 SetPreDefinedOpts() 61 assert.Nil(t, preDefinedOpts) 62 assert.DeepEqual(t, time.Duration(0), opt.WriteTimeout()) 63 assert.DeepEqual(t, time.Duration(0), opt.ReadTimeout()) 64 assert.DeepEqual(t, time.Duration(0), opt.DialTimeout()) 65 } 66 67 // TestRequestOptions_CopyTo test request options copy to another one 68 func TestRequestOptions_CopyTo(t *testing.T) { 69 opt := NewRequestOptions([]RequestOption{ 70 WithTag("a", "b"), 71 WithSD(false), 72 }) 73 var copyOpt RequestOptions 74 opt.CopyTo(©Opt) 75 assert.DeepEqual(t, opt.Tags(), copyOpt.Tags()) 76 assert.DeepEqual(t, opt.IsSD(), copyOpt.IsSD()) 77 }