media_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015 Google Inc. 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 gensupport
  5. import (
  6. "bytes"
  7. "io"
  8. "io/ioutil"
  9. "reflect"
  10. "testing"
  11. )
  12. func TestContentSniffing(t *testing.T) {
  13. type testCase struct {
  14. data []byte // the data to read from the Reader
  15. finalErr error // error to return after data has been read
  16. wantContentType string
  17. wantContentTypeResult bool
  18. }
  19. for _, tc := range []testCase{
  20. {
  21. data: []byte{0, 0, 0, 0},
  22. finalErr: nil,
  23. wantContentType: "application/octet-stream",
  24. wantContentTypeResult: true,
  25. },
  26. {
  27. data: []byte(""),
  28. finalErr: nil,
  29. wantContentType: "text/plain; charset=utf-8",
  30. wantContentTypeResult: true,
  31. },
  32. {
  33. data: []byte(""),
  34. finalErr: io.ErrUnexpectedEOF,
  35. wantContentType: "text/plain; charset=utf-8",
  36. wantContentTypeResult: false,
  37. },
  38. {
  39. data: []byte("abc"),
  40. finalErr: nil,
  41. wantContentType: "text/plain; charset=utf-8",
  42. wantContentTypeResult: true,
  43. },
  44. {
  45. data: []byte("abc"),
  46. finalErr: io.ErrUnexpectedEOF,
  47. wantContentType: "text/plain; charset=utf-8",
  48. wantContentTypeResult: false,
  49. },
  50. // The following examples contain more bytes than are buffered for sniffing.
  51. {
  52. data: bytes.Repeat([]byte("a"), 513),
  53. finalErr: nil,
  54. wantContentType: "text/plain; charset=utf-8",
  55. wantContentTypeResult: true,
  56. },
  57. {
  58. data: bytes.Repeat([]byte("a"), 513),
  59. finalErr: io.ErrUnexpectedEOF,
  60. wantContentType: "text/plain; charset=utf-8",
  61. wantContentTypeResult: true, // true because error is after first 512 bytes.
  62. },
  63. } {
  64. er := &errReader{buf: tc.data, err: tc.finalErr}
  65. sct := newContentSniffer(er)
  66. // Even if was an error during the first 512 bytes, we should still be able to read those bytes.
  67. buf, err := ioutil.ReadAll(sct)
  68. if !reflect.DeepEqual(buf, tc.data) {
  69. t.Fatalf("Failed reading buffer: got: %q; want:%q", buf, tc.data)
  70. }
  71. if err != tc.finalErr {
  72. t.Fatalf("Reading buffer error: got: %v; want: %v", err, tc.finalErr)
  73. }
  74. ct, ok := sct.ContentType()
  75. if ok != tc.wantContentTypeResult {
  76. t.Fatalf("Content type result got: %v; want: %v", ok, tc.wantContentTypeResult)
  77. }
  78. if ok && ct != tc.wantContentType {
  79. t.Fatalf("Content type got: %q; want: %q", ct, tc.wantContentType)
  80. }
  81. }
  82. }
  83. type staticContentTyper struct {
  84. io.Reader
  85. }
  86. func (sct staticContentTyper) ContentType() string {
  87. return "static content type"
  88. }
  89. func TestDetermineContentType(t *testing.T) {
  90. data := []byte("abc")
  91. rdr := func() io.Reader {
  92. return bytes.NewBuffer(data)
  93. }
  94. type testCase struct {
  95. r io.Reader
  96. explicitConentType string
  97. wantContentType string
  98. }
  99. for _, tc := range []testCase{
  100. {
  101. r: rdr(),
  102. wantContentType: "text/plain; charset=utf-8",
  103. },
  104. {
  105. r: staticContentTyper{rdr()},
  106. wantContentType: "static content type",
  107. },
  108. {
  109. r: staticContentTyper{rdr()},
  110. explicitConentType: "explicit",
  111. wantContentType: "explicit",
  112. },
  113. } {
  114. r, ctype := DetermineContentType(tc.r, tc.explicitConentType)
  115. got, err := ioutil.ReadAll(r)
  116. if err != nil {
  117. t.Fatalf("Failed reading buffer: %v", err)
  118. }
  119. if !reflect.DeepEqual(got, data) {
  120. t.Fatalf("Failed reading buffer: got: %q; want:%q", got, data)
  121. }
  122. if ctype != tc.wantContentType {
  123. t.Fatalf("Content type got: %q; want: %q", ctype, tc.wantContentType)
  124. }
  125. }
  126. }