github.com/astaxie/beego@v1.12.3/config/json_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 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 config 16 17 import ( 18 "fmt" 19 "os" 20 "testing" 21 ) 22 23 func TestJsonStartsWithArray(t *testing.T) { 24 25 const jsoncontextwitharray = `[ 26 { 27 "url": "user", 28 "serviceAPI": "http://www.test.com/user" 29 }, 30 { 31 "url": "employee", 32 "serviceAPI": "http://www.test.com/employee" 33 } 34 ]` 35 f, err := os.Create("testjsonWithArray.conf") 36 if err != nil { 37 t.Fatal(err) 38 } 39 _, err = f.WriteString(jsoncontextwitharray) 40 if err != nil { 41 f.Close() 42 t.Fatal(err) 43 } 44 f.Close() 45 defer os.Remove("testjsonWithArray.conf") 46 jsonconf, err := NewConfig("json", "testjsonWithArray.conf") 47 if err != nil { 48 t.Fatal(err) 49 } 50 rootArray, err := jsonconf.DIY("rootArray") 51 if err != nil { 52 t.Error("array does not exist as element") 53 } 54 rootArrayCasted := rootArray.([]interface{}) 55 if rootArrayCasted == nil { 56 t.Error("array from root is nil") 57 } else { 58 elem := rootArrayCasted[0].(map[string]interface{}) 59 if elem["url"] != "user" || elem["serviceAPI"] != "http://www.test.com/user" { 60 t.Error("array[0] values are not valid") 61 } 62 63 elem2 := rootArrayCasted[1].(map[string]interface{}) 64 if elem2["url"] != "employee" || elem2["serviceAPI"] != "http://www.test.com/employee" { 65 t.Error("array[1] values are not valid") 66 } 67 } 68 } 69 70 func TestJson(t *testing.T) { 71 72 var ( 73 jsoncontext = `{ 74 "appname": "beeapi", 75 "testnames": "foo;bar", 76 "httpport": 8080, 77 "mysqlport": 3600, 78 "PI": 3.1415976, 79 "runmode": "dev", 80 "autorender": false, 81 "copyrequestbody": true, 82 "session": "on", 83 "cookieon": "off", 84 "newreg": "OFF", 85 "needlogin": "ON", 86 "enableSession": "Y", 87 "enableCookie": "N", 88 "flag": 1, 89 "path1": "${GOPATH}", 90 "path2": "${GOPATH||/home/go}", 91 "database": { 92 "host": "host", 93 "port": "port", 94 "database": "database", 95 "username": "username", 96 "password": "${GOPATH}", 97 "conns":{ 98 "maxconnection":12, 99 "autoconnect":true, 100 "connectioninfo":"info", 101 "root": "${GOPATH}" 102 } 103 } 104 }` 105 keyValue = map[string]interface{}{ 106 "appname": "beeapi", 107 "testnames": []string{"foo", "bar"}, 108 "httpport": 8080, 109 "mysqlport": int64(3600), 110 "PI": 3.1415976, 111 "runmode": "dev", 112 "autorender": false, 113 "copyrequestbody": true, 114 "session": true, 115 "cookieon": false, 116 "newreg": false, 117 "needlogin": true, 118 "enableSession": true, 119 "enableCookie": false, 120 "flag": true, 121 "path1": os.Getenv("GOPATH"), 122 "path2": os.Getenv("GOPATH"), 123 "database::host": "host", 124 "database::port": "port", 125 "database::database": "database", 126 "database::password": os.Getenv("GOPATH"), 127 "database::conns::maxconnection": 12, 128 "database::conns::autoconnect": true, 129 "database::conns::connectioninfo": "info", 130 "database::conns::root": os.Getenv("GOPATH"), 131 "unknown": "", 132 } 133 ) 134 135 f, err := os.Create("testjson.conf") 136 if err != nil { 137 t.Fatal(err) 138 } 139 _, err = f.WriteString(jsoncontext) 140 if err != nil { 141 f.Close() 142 t.Fatal(err) 143 } 144 f.Close() 145 defer os.Remove("testjson.conf") 146 jsonconf, err := NewConfig("json", "testjson.conf") 147 if err != nil { 148 t.Fatal(err) 149 } 150 151 for k, v := range keyValue { 152 var err error 153 var value interface{} 154 switch v.(type) { 155 case int: 156 value, err = jsonconf.Int(k) 157 case int64: 158 value, err = jsonconf.Int64(k) 159 case float64: 160 value, err = jsonconf.Float(k) 161 case bool: 162 value, err = jsonconf.Bool(k) 163 case []string: 164 value = jsonconf.Strings(k) 165 case string: 166 value = jsonconf.String(k) 167 default: 168 value, err = jsonconf.DIY(k) 169 } 170 if err != nil { 171 t.Fatalf("get key %q value fatal,%v err %s", k, v, err) 172 } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) { 173 t.Fatalf("get key %q value, want %v got %v .", k, v, value) 174 } 175 176 } 177 if err = jsonconf.Set("name", "astaxie"); err != nil { 178 t.Fatal(err) 179 } 180 if jsonconf.String("name") != "astaxie" { 181 t.Fatal("get name error") 182 } 183 184 if db, err := jsonconf.DIY("database"); err != nil { 185 t.Fatal(err) 186 } else if m, ok := db.(map[string]interface{}); !ok { 187 t.Log(db) 188 t.Fatal("db not map[string]interface{}") 189 } else { 190 if m["host"].(string) != "host" { 191 t.Fatal("get host err") 192 } 193 } 194 195 if _, err := jsonconf.Int("unknown"); err == nil { 196 t.Error("unknown keys should return an error when expecting an Int") 197 } 198 199 if _, err := jsonconf.Int64("unknown"); err == nil { 200 t.Error("unknown keys should return an error when expecting an Int64") 201 } 202 203 if _, err := jsonconf.Float("unknown"); err == nil { 204 t.Error("unknown keys should return an error when expecting a Float") 205 } 206 207 if _, err := jsonconf.DIY("unknown"); err == nil { 208 t.Error("unknown keys should return an error when expecting an interface{}") 209 } 210 211 if val := jsonconf.String("unknown"); val != "" { 212 t.Error("unknown keys should return an empty string when expecting a String") 213 } 214 215 if _, err := jsonconf.Bool("unknown"); err == nil { 216 t.Error("unknown keys should return an error when expecting a Bool") 217 } 218 219 if !jsonconf.DefaultBool("unknown", true) { 220 t.Error("unknown keys with default value wrong") 221 } 222 }