load_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2018 Frank Schroeder. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package properties
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "strings"
  12. "testing"
  13. "github.com/magiconair/properties/assert"
  14. )
  15. func TestEncoding(t *testing.T) {
  16. if got, want := utf8Default, Encoding(0); got != want {
  17. t.Fatalf("got encoding %d want %d", got, want)
  18. }
  19. if got, want := UTF8, Encoding(1); got != want {
  20. t.Fatalf("got encoding %d want %d", got, want)
  21. }
  22. if got, want := ISO_8859_1, Encoding(2); got != want {
  23. t.Fatalf("got encoding %d want %d", got, want)
  24. }
  25. }
  26. func TestLoadFailsWithNotExistingFile(t *testing.T) {
  27. _, err := LoadFile("doesnotexist.properties", ISO_8859_1)
  28. assert.Equal(t, err != nil, true, "")
  29. assert.Matches(t, err.Error(), "open.*no such file or directory")
  30. }
  31. func TestLoadFilesFailsOnNotExistingFile(t *testing.T) {
  32. _, err := LoadFile("doesnotexist.properties", ISO_8859_1)
  33. assert.Equal(t, err != nil, true, "")
  34. assert.Matches(t, err.Error(), "open.*no such file or directory")
  35. }
  36. func TestLoadFilesDoesNotFailOnNotExistingFileAndIgnoreMissing(t *testing.T) {
  37. p, err := LoadFiles([]string{"doesnotexist.properties"}, ISO_8859_1, true)
  38. assert.Equal(t, err, nil)
  39. assert.Equal(t, p.Len(), 0)
  40. }
  41. func TestLoadString(t *testing.T) {
  42. x := "key=äüö"
  43. p1 := MustLoadString(x)
  44. p2 := must(Load([]byte(x), UTF8))
  45. assert.Equal(t, p1, p2)
  46. }
  47. func TestLoadMap(t *testing.T) {
  48. // LoadMap does not guarantee the same import order
  49. // of keys every time since map access is randomized.
  50. // Therefore, we need to compare the generated maps.
  51. m := map[string]string{"key": "value", "abc": "def"}
  52. assert.Equal(t, LoadMap(m).Map(), m)
  53. }
  54. func TestLoadFile(t *testing.T) {
  55. tf := make(tempFiles, 0)
  56. defer tf.removeAll()
  57. filename := tf.makeFile("key=value")
  58. p := MustLoadFile(filename, ISO_8859_1)
  59. assert.Equal(t, p.Len(), 1)
  60. assertKeyValues(t, "", p, "key", "value")
  61. }
  62. func TestLoadFiles(t *testing.T) {
  63. tf := make(tempFiles, 0)
  64. defer tf.removeAll()
  65. filename := tf.makeFile("key=value")
  66. filename2 := tf.makeFile("key2=value2")
  67. p := MustLoadFiles([]string{filename, filename2}, ISO_8859_1, false)
  68. assertKeyValues(t, "", p, "key", "value", "key2", "value2")
  69. }
  70. func TestLoadExpandedFile(t *testing.T) {
  71. tf := make(tempFiles, 0)
  72. defer tf.removeAll()
  73. if err := os.Setenv("_VARX", "some-value"); err != nil {
  74. t.Fatal(err)
  75. }
  76. filename := tf.makeFilePrefix(os.Getenv("_VARX"), "key=value")
  77. filename = strings.Replace(filename, os.Getenv("_VARX"), "${_VARX}", -1)
  78. p := MustLoadFile(filename, ISO_8859_1)
  79. assertKeyValues(t, "", p, "key", "value")
  80. }
  81. func TestLoadFilesAndIgnoreMissing(t *testing.T) {
  82. tf := make(tempFiles, 0)
  83. defer tf.removeAll()
  84. filename := tf.makeFile("key=value")
  85. filename2 := tf.makeFile("key2=value2")
  86. p := MustLoadFiles([]string{filename, filename + "foo", filename2, filename2 + "foo"}, ISO_8859_1, true)
  87. assertKeyValues(t, "", p, "key", "value", "key2", "value2")
  88. }
  89. func TestLoadURL(t *testing.T) {
  90. srv := testServer()
  91. defer srv.Close()
  92. p := MustLoadURL(srv.URL + "/a")
  93. assertKeyValues(t, "", p, "key", "value")
  94. }
  95. func TestLoadURLs(t *testing.T) {
  96. srv := testServer()
  97. defer srv.Close()
  98. p := MustLoadURLs([]string{srv.URL + "/a", srv.URL + "/b"}, false)
  99. assertKeyValues(t, "", p, "key", "value", "key2", "value2")
  100. }
  101. func TestLoadURLsAndFailMissing(t *testing.T) {
  102. srv := testServer()
  103. defer srv.Close()
  104. p, err := LoadURLs([]string{srv.URL + "/a", srv.URL + "/c"}, false)
  105. assert.Equal(t, p, (*Properties)(nil))
  106. assert.Matches(t, err.Error(), ".*returned 404.*")
  107. }
  108. func TestLoadURLsAndIgnoreMissing(t *testing.T) {
  109. srv := testServer()
  110. defer srv.Close()
  111. p := MustLoadURLs([]string{srv.URL + "/a", srv.URL + "/b", srv.URL + "/c"}, true)
  112. assertKeyValues(t, "", p, "key", "value", "key2", "value2")
  113. }
  114. func TestLoadURLEncoding(t *testing.T) {
  115. srv := testServer()
  116. defer srv.Close()
  117. uris := []string{"/none", "/utf8", "/plain", "/latin1", "/iso88591"}
  118. for i, uri := range uris {
  119. p := MustLoadURL(srv.URL + uri)
  120. assert.Equal(t, p.GetString("key", ""), "äöü", fmt.Sprintf("%d", i))
  121. }
  122. }
  123. func TestLoadURLFailInvalidEncoding(t *testing.T) {
  124. srv := testServer()
  125. defer srv.Close()
  126. p, err := LoadURL(srv.URL + "/json")
  127. assert.Equal(t, p, (*Properties)(nil))
  128. assert.Matches(t, err.Error(), ".*invalid content type.*")
  129. }
  130. func TestLoadAll(t *testing.T) {
  131. tf := make(tempFiles, 0)
  132. defer tf.removeAll()
  133. filename := tf.makeFile("key=value")
  134. filename2 := tf.makeFile("key2=value3")
  135. filename3 := tf.makeFile("key=value4")
  136. srv := testServer()
  137. defer srv.Close()
  138. p := MustLoadAll([]string{filename, filename2, srv.URL + "/a", srv.URL + "/b", filename3}, UTF8, false)
  139. assertKeyValues(t, "", p, "key", "value4", "key2", "value2")
  140. }
  141. type tempFiles []string
  142. func (tf *tempFiles) removeAll() {
  143. for _, path := range *tf {
  144. err := os.Remove(path)
  145. if err != nil {
  146. fmt.Printf("os.Remove: %v", err)
  147. }
  148. }
  149. }
  150. func (tf *tempFiles) makeFile(data string) string {
  151. return tf.makeFilePrefix("properties", data)
  152. }
  153. func (tf *tempFiles) makeFilePrefix(prefix, data string) string {
  154. f, err := ioutil.TempFile("", prefix)
  155. if err != nil {
  156. panic("ioutil.TempFile: " + err.Error())
  157. }
  158. // remember the temp file so that we can remove it later
  159. *tf = append(*tf, f.Name())
  160. n, err := fmt.Fprint(f, data)
  161. if err != nil {
  162. panic("fmt.Fprintln: " + err.Error())
  163. }
  164. if n != len(data) {
  165. panic(fmt.Sprintf("Data size mismatch. expected=%d wrote=%d\n", len(data), n))
  166. }
  167. err = f.Close()
  168. if err != nil {
  169. panic("f.Close: " + err.Error())
  170. }
  171. return f.Name()
  172. }
  173. func testServer() *httptest.Server {
  174. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  175. send := func(data []byte, contentType string) {
  176. w.Header().Set("Content-Type", contentType)
  177. if _, err := w.Write(data); err != nil {
  178. panic(err)
  179. }
  180. }
  181. utf8 := []byte("key=äöü")
  182. iso88591 := []byte{0x6b, 0x65, 0x79, 0x3d, 0xe4, 0xf6, 0xfc} // key=äöü
  183. switch r.RequestURI {
  184. case "/a":
  185. send([]byte("key=value"), "")
  186. case "/b":
  187. send([]byte("key2=value2"), "")
  188. case "/none":
  189. send(utf8, "")
  190. case "/utf8":
  191. send(utf8, "text/plain; charset=utf-8")
  192. case "/json":
  193. send(utf8, "application/json; charset=utf-8")
  194. case "/plain":
  195. send(iso88591, "text/plain")
  196. case "/latin1":
  197. send(iso88591, "text/plain; charset=latin1")
  198. case "/iso88591":
  199. send(iso88591, "text/plain; charset=iso-8859-1")
  200. default:
  201. w.WriteHeader(404)
  202. }
  203. }))
  204. }