1
0

staticfile_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package beego
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "compress/zlib"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. )
  12. var currentWorkDir, _ = os.Getwd()
  13. var licenseFile = filepath.Join(currentWorkDir, "LICENSE")
  14. func testOpenFile(encoding string, content []byte, t *testing.T) {
  15. fi, _ := os.Stat(licenseFile)
  16. b, n, sch, err := openFile(licenseFile, fi, encoding)
  17. if err != nil {
  18. t.Log(err)
  19. t.Fail()
  20. }
  21. t.Log("open static file encoding "+n, b)
  22. assetOpenFileAndContent(sch, content, t)
  23. }
  24. func TestOpenStaticFile_1(t *testing.T) {
  25. file, _ := os.Open(licenseFile)
  26. content, _ := ioutil.ReadAll(file)
  27. testOpenFile("", content, t)
  28. }
  29. func TestOpenStaticFileGzip_1(t *testing.T) {
  30. file, _ := os.Open(licenseFile)
  31. var zipBuf bytes.Buffer
  32. fileWriter, _ := gzip.NewWriterLevel(&zipBuf, gzip.BestCompression)
  33. io.Copy(fileWriter, file)
  34. fileWriter.Close()
  35. content, _ := ioutil.ReadAll(&zipBuf)
  36. testOpenFile("gzip", content, t)
  37. }
  38. func TestOpenStaticFileDeflate_1(t *testing.T) {
  39. file, _ := os.Open(licenseFile)
  40. var zipBuf bytes.Buffer
  41. fileWriter, _ := zlib.NewWriterLevel(&zipBuf, zlib.BestCompression)
  42. io.Copy(fileWriter, file)
  43. fileWriter.Close()
  44. content, _ := ioutil.ReadAll(&zipBuf)
  45. testOpenFile("deflate", content, t)
  46. }
  47. func assetOpenFileAndContent(sch *serveContentHolder, content []byte, t *testing.T) {
  48. t.Log(sch.size, len(content))
  49. if sch.size != int64(len(content)) {
  50. t.Log("static content file size not same")
  51. t.Fail()
  52. }
  53. bs, _ := ioutil.ReadAll(sch)
  54. for i, v := range content {
  55. if v != bs[i] {
  56. t.Log("content not same")
  57. t.Fail()
  58. }
  59. }
  60. if len(staticFileMap) == 0 {
  61. t.Log("men map is empty")
  62. t.Fail()
  63. }
  64. }