github.com/abolfazlbeh/zhycan@v0.0.0-20230819144214-24cf38237387/internal/http/types_test.go (about) 1 package http 2 3 import ( 4 "encoding/json" 5 "github.com/gofiber/fiber/v2" 6 "reflect" 7 "testing" 8 ) 9 10 func TestServerConfig_UnmarshalJson(t *testing.T) { 11 // Test input data 12 input := []byte(`{ 13 "addr": ":3000", 14 "name": "s1", 15 "versions": ["v1", "v2"], 16 "support_static": true, 17 "conf": { 18 "server_header": "", 19 "strict_routing": false, 20 "case_sensitive": false, 21 "unescape_path": false, 22 "etag": false, 23 "body_limit": 4194304, 24 "concurrency": 262144, 25 "read_timeout": -1, 26 "write_timeout": -1, 27 "idle_timeout": -1, 28 "read_buffer_size": 4096, 29 "write_buffer_size": 4096, 30 "compressed_file_suffix": ".gz", 31 "get_only": false, 32 "disable_keepalive": false, 33 "network": "tcp", 34 "enable_print_routes": true, 35 "attach_error_handler": true, 36 "request_methods": ["ALL"] 37 }, 38 "middlewares": { 39 "order": ["logger", "favicon"], 40 "favicon": { 41 "file": "./favicon.ico", 42 "url": "/favicon.ico", 43 "cache_control": "public, max-age=31536000" 44 } 45 }, 46 "static": { 47 "prefix": "/", 48 "root": "./public", 49 "config": { 50 "compress": false, 51 "byte_range": false, 52 "browse": false, 53 "download": false, 54 "index": "index.html", 55 "cache_duration": 10, 56 "max_age": 0 57 } 58 } 59 }`) 60 61 // Test expected result 62 v := make([]string, 2) 63 v[0] = "v1" 64 v[1] = "v2" 65 expected := ServerConfig{ 66 ListenAddress: ":3000", 67 Name: "s1", 68 Versions: v, 69 SupportStatic: true, 70 Config: struct { 71 ServerHeader string `json:"server_header"` 72 StrictRouting bool `json:"strict_routing"` 73 CaseSensitive bool `json:"case_sensitive"` 74 UnescapePath bool `json:"unescape_path"` 75 Etag bool `json:"etag"` 76 BodyLimit int `json:"body_limit"` 77 Concurrency int `json:"concurrency"` 78 ReadTimeout int `json:"read_timeout"` 79 WriteTimeout int `json:"write_timeout"` 80 IdleTimeout int `json:"idle_timeout"` 81 ReadBufferSize int `json:"read_buffer_size"` 82 WriteBufferSize int `json:"write_buffer_size"` 83 CompressedFileSuffix string `json:"compressed_file_suffix"` 84 GetOnly bool `json:"get_only"` 85 DisableKeepalive bool `json:"disable_keepalive"` 86 Network string `json:"network"` 87 EnablePrintRoutes bool `json:"enable_print_routes"` 88 AttachErrorHandler bool `json:"attach_error_handler"` 89 RequestMethods []string `json:"request_methods"` 90 }{ 91 ServerHeader: "", 92 StrictRouting: false, 93 CaseSensitive: false, 94 UnescapePath: false, 95 Etag: false, 96 BodyLimit: 4194304, 97 Concurrency: 262144, 98 ReadTimeout: -1, 99 WriteTimeout: -1, 100 IdleTimeout: -1, 101 ReadBufferSize: 4096, 102 WriteBufferSize: 4096, 103 CompressedFileSuffix: ".gz", 104 GetOnly: false, 105 DisableKeepalive: false, 106 Network: "tcp", 107 EnablePrintRoutes: true, 108 AttachErrorHandler: true, 109 RequestMethods: []string{"ALL"}, 110 }, 111 Middlewares: struct { 112 Order []string `json:"order"` 113 }{Order: []string{"logger", "favicon"}}, 114 Static: struct { 115 Prefix string `json:"prefix"` 116 Root string `json:"root"` 117 Config fiber.Static `json:"config"` 118 }{Prefix: "/", Root: "./public", Config: fiber.Static{ 119 Compress: false, 120 ByteRange: false, 121 Browse: false, 122 Download: false, 123 Index: "index.html", 124 CacheDuration: 10, 125 MaxAge: 0, 126 }}, 127 } 128 129 // Unmarshal the input data into a ServerConfig instance 130 var config ServerConfig 131 err := json.Unmarshal(input, &config) 132 if err != nil { 133 t.Errorf("Error unmarshaling JSON: %v", err) 134 } 135 136 // Check if the result is what we expected 137 if !reflect.DeepEqual(config, expected) { 138 t.Errorf("Expected %+v, got %+v", expected, config) 139 } 140 }