github.com/cloudwego/hertz@v0.9.3/pkg/app/server/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 server
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  	"reflect"
    23  	"syscall"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/cloudwego/hertz/pkg/app/server/registry"
    28  	"github.com/cloudwego/hertz/pkg/common/config"
    29  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    30  	"github.com/cloudwego/hertz/pkg/common/tracer/stats"
    31  	"github.com/cloudwego/hertz/pkg/common/utils"
    32  	"github.com/cloudwego/hertz/pkg/network"
    33  )
    34  
    35  func TestOptions(t *testing.T) {
    36  	info := &registry.Info{
    37  		ServiceName: "hertz.test.api",
    38  		Weight:      10,
    39  		Addr:        utils.NewNetAddr("tcp", ":8888"),
    40  	}
    41  	cfg := &net.ListenConfig{Control: func(network, address string, c syscall.RawConn) error {
    42  		return c.Control(func(fd uintptr) {})
    43  	}}
    44  	transporter := func(options *config.Options) network.Transporter {
    45  		return &mockTransporter{}
    46  	}
    47  	opt := config.NewOptions([]config.Option{
    48  		WithReadTimeout(time.Second),
    49  		WithWriteTimeout(time.Second),
    50  		WithIdleTimeout(time.Second),
    51  		WithKeepAliveTimeout(time.Second),
    52  		WithRedirectTrailingSlash(false),
    53  		WithRedirectFixedPath(true),
    54  		WithHandleMethodNotAllowed(true),
    55  		WithUseRawPath(true),
    56  		WithRemoveExtraSlash(true),
    57  		WithUnescapePathValues(false),
    58  		WithDisablePreParseMultipartForm(true),
    59  		WithStreamBody(false),
    60  		WithHostPorts(":8888"),
    61  		WithBasePath("/"),
    62  		WithMaxRequestBodySize(2),
    63  		WithDisablePrintRoute(true),
    64  		WithSenseClientDisconnection(true),
    65  		WithNetwork("unix"),
    66  		WithExitWaitTime(time.Second),
    67  		WithMaxKeepBodySize(500),
    68  		WithGetOnly(true),
    69  		WithKeepAlive(false),
    70  		WithTLS(nil),
    71  		WithH2C(true),
    72  		WithReadBufferSize(100),
    73  		WithALPN(true),
    74  		WithTraceLevel(stats.LevelDisabled),
    75  		WithRegistry(nil, info),
    76  		WithAutoReloadRender(true, 5*time.Second),
    77  		WithListenConfig(cfg),
    78  		WithAltTransport(transporter),
    79  		WithDisableHeaderNamesNormalizing(true),
    80  	})
    81  	assert.DeepEqual(t, opt.ReadTimeout, time.Second)
    82  	assert.DeepEqual(t, opt.WriteTimeout, time.Second)
    83  	assert.DeepEqual(t, opt.IdleTimeout, time.Second)
    84  	assert.DeepEqual(t, opt.KeepAliveTimeout, time.Second)
    85  	assert.DeepEqual(t, opt.RedirectTrailingSlash, false)
    86  	assert.DeepEqual(t, opt.RedirectFixedPath, true)
    87  	assert.DeepEqual(t, opt.HandleMethodNotAllowed, true)
    88  	assert.DeepEqual(t, opt.UseRawPath, true)
    89  	assert.DeepEqual(t, opt.RemoveExtraSlash, true)
    90  	assert.DeepEqual(t, opt.UnescapePathValues, false)
    91  	assert.DeepEqual(t, opt.DisablePreParseMultipartForm, true)
    92  	assert.DeepEqual(t, opt.StreamRequestBody, false)
    93  	assert.DeepEqual(t, opt.Addr, ":8888")
    94  	assert.DeepEqual(t, opt.BasePath, "/")
    95  	assert.DeepEqual(t, opt.MaxRequestBodySize, 2)
    96  	assert.DeepEqual(t, opt.DisablePrintRoute, true)
    97  	assert.DeepEqual(t, opt.SenseClientDisconnection, true)
    98  	assert.DeepEqual(t, opt.Network, "unix")
    99  	assert.DeepEqual(t, opt.ExitWaitTimeout, time.Second)
   100  	assert.DeepEqual(t, opt.MaxKeepBodySize, 500)
   101  	assert.DeepEqual(t, opt.GetOnly, true)
   102  	assert.DeepEqual(t, opt.DisableKeepalive, true)
   103  	assert.DeepEqual(t, opt.H2C, true)
   104  	assert.DeepEqual(t, opt.ReadBufferSize, 100)
   105  	assert.DeepEqual(t, opt.ALPN, true)
   106  	assert.DeepEqual(t, opt.TraceLevel, stats.LevelDisabled)
   107  	assert.DeepEqual(t, opt.RegistryInfo, info)
   108  	assert.DeepEqual(t, opt.Registry, nil)
   109  	assert.DeepEqual(t, opt.AutoReloadRender, true)
   110  	assert.DeepEqual(t, opt.AutoReloadInterval, 5*time.Second)
   111  	assert.DeepEqual(t, opt.ListenConfig, cfg)
   112  	assert.Assert(t, reflect.TypeOf(opt.AltTransporterNewer) == reflect.TypeOf(transporter))
   113  	assert.DeepEqual(t, opt.DisableHeaderNamesNormalizing, true)
   114  }
   115  
   116  func TestDefaultOptions(t *testing.T) {
   117  	opt := config.NewOptions([]config.Option{})
   118  	assert.DeepEqual(t, opt.ReadTimeout, time.Minute*3)
   119  	assert.DeepEqual(t, opt.IdleTimeout, time.Minute*3)
   120  	assert.DeepEqual(t, opt.KeepAliveTimeout, time.Minute)
   121  	assert.DeepEqual(t, opt.RedirectTrailingSlash, true)
   122  	assert.DeepEqual(t, opt.RedirectFixedPath, false)
   123  	assert.DeepEqual(t, opt.HandleMethodNotAllowed, false)
   124  	assert.DeepEqual(t, opt.UseRawPath, false)
   125  	assert.DeepEqual(t, opt.RemoveExtraSlash, false)
   126  	assert.DeepEqual(t, opt.UnescapePathValues, true)
   127  	assert.DeepEqual(t, opt.DisablePreParseMultipartForm, false)
   128  	assert.DeepEqual(t, opt.StreamRequestBody, false)
   129  	assert.DeepEqual(t, opt.Addr, ":8888")
   130  	assert.DeepEqual(t, opt.BasePath, "/")
   131  	assert.DeepEqual(t, opt.MaxRequestBodySize, 4*1024*1024)
   132  	assert.DeepEqual(t, opt.GetOnly, false)
   133  	assert.DeepEqual(t, opt.DisableKeepalive, false)
   134  	assert.DeepEqual(t, opt.DisablePrintRoute, false)
   135  	assert.DeepEqual(t, opt.SenseClientDisconnection, false)
   136  	assert.DeepEqual(t, opt.Network, "tcp")
   137  	assert.DeepEqual(t, opt.ExitWaitTimeout, time.Second*5)
   138  	assert.DeepEqual(t, opt.MaxKeepBodySize, 4*1024*1024)
   139  	assert.DeepEqual(t, opt.H2C, false)
   140  	assert.DeepEqual(t, opt.ReadBufferSize, 4096)
   141  	assert.DeepEqual(t, opt.ALPN, false)
   142  	assert.DeepEqual(t, opt.Registry, registry.NoopRegistry)
   143  	assert.DeepEqual(t, opt.AutoReloadRender, false)
   144  	assert.Assert(t, opt.RegistryInfo == nil)
   145  	assert.DeepEqual(t, opt.AutoReloadRender, false)
   146  	assert.DeepEqual(t, opt.AutoReloadInterval, time.Duration(0))
   147  	assert.DeepEqual(t, opt.DisableHeaderNamesNormalizing, false)
   148  }
   149  
   150  type mockTransporter struct{}
   151  
   152  func (m *mockTransporter) ListenAndServe(onData network.OnData) (err error) {
   153  	panic("implement me")
   154  }
   155  
   156  func (m *mockTransporter) Close() error {
   157  	panic("implement me")
   158  }
   159  
   160  func (m *mockTransporter) Shutdown(ctx context.Context) error {
   161  	panic("implement me")
   162  }