You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

237 lines
5.5 KiB

  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package talksapp
  7. import (
  8. "fmt"
  9. "net/http"
  10. "net/http/httptest"
  11. "net/url"
  12. "strings"
  13. "testing"
  14. "time"
  15. "google.golang.org/appengine"
  16. "google.golang.org/appengine/aetest"
  17. "google.golang.org/appengine/memcache"
  18. "github.com/golang/gddo/gosrc"
  19. )
  20. const importPath = "github.com/user/repo/path/to/presentation.slide"
  21. func TestHome(t *testing.T) {
  22. do(t, "GET", "/", func(r *http.Request) {
  23. w := httptest.NewRecorder()
  24. handlerFunc(serveRoot).ServeHTTP(w, r)
  25. if w.Code != http.StatusOK {
  26. t.Fatalf("expected status: %d, got: %d", http.StatusOK, w.Code)
  27. }
  28. if !strings.Contains(w.Body.String(), "go-talks.appspot.org") {
  29. t.Fatal("expected response to contain: go-talks.appspot.org")
  30. }
  31. })
  32. }
  33. func TestPresentation(t *testing.T) {
  34. presentationTitle := "My awesome presentation!"
  35. presentationSrc := []byte(presentationTitle + `
  36. Subtitle
  37. * Slide 1
  38. - Foo
  39. - Bar
  40. - Baz
  41. `)
  42. originalGetPresentation := getPresentation
  43. getPresentation = func(client *http.Client, importPath string) (*gosrc.Presentation, error) {
  44. return &gosrc.Presentation{
  45. Filename: "presentation.slide",
  46. Files: map[string][]byte{
  47. "presentation.slide": presentationSrc,
  48. },
  49. }, nil
  50. }
  51. defer func() {
  52. getPresentation = originalGetPresentation
  53. }()
  54. do(t, "GET", "/"+importPath, func(r *http.Request) {
  55. w := httptest.NewRecorder()
  56. handlerFunc(serveRoot).ServeHTTP(w, r)
  57. if w.Code != http.StatusOK {
  58. t.Fatalf("expected status: %d, got: %d", http.StatusOK, w.Code)
  59. }
  60. if !strings.Contains(w.Body.String(), presentationTitle) {
  61. t.Fatalf("unexpected response body: %s", w.Body)
  62. }
  63. c := appengine.NewContext(r)
  64. _, err := memcache.Get(c, importPath)
  65. if err == memcache.ErrCacheMiss {
  66. t.Fatal("expected result to be cached")
  67. }
  68. if err != nil {
  69. t.Fatalf("expected no error, got: %s", err)
  70. }
  71. })
  72. }
  73. func TestPresentationCacheHit(t *testing.T) {
  74. do(t, "GET", "/"+importPath, func(r *http.Request) {
  75. cachedPresentation := "<div>My Presentation</div>"
  76. c := appengine.NewContext(r)
  77. memcache.Add(c, &memcache.Item{
  78. Key: importPath,
  79. Value: []byte(cachedPresentation),
  80. Expiration: time.Hour,
  81. })
  82. w := httptest.NewRecorder()
  83. handlerFunc(serveRoot).ServeHTTP(w, r)
  84. if w.Code != http.StatusOK {
  85. t.Fatalf("expected status: %d, got: %d", http.StatusOK, w.Code)
  86. }
  87. if w.Body.String() != cachedPresentation {
  88. t.Fatal("response does not matched cached presentation")
  89. }
  90. })
  91. }
  92. func TestPresentationNotFound(t *testing.T) {
  93. originalGetPresentation := getPresentation
  94. getPresentation = func(client *http.Client, importPath string) (*gosrc.Presentation, error) {
  95. return nil, gosrc.NotFoundError{}
  96. }
  97. defer func() {
  98. getPresentation = originalGetPresentation
  99. }()
  100. do(t, "GET", "/"+importPath, func(r *http.Request) {
  101. w := httptest.NewRecorder()
  102. handlerFunc(serveRoot).ServeHTTP(w, r)
  103. if w.Code != http.StatusBadRequest {
  104. t.Fatalf("expected status: %d, got: %d", http.StatusBadRequest, w.Code)
  105. }
  106. })
  107. }
  108. func TestWrongMethod(t *testing.T) {
  109. do(t, "POST", "/", func(r *http.Request) {
  110. w := httptest.NewRecorder()
  111. handlerFunc(serveRoot).ServeHTTP(w, r)
  112. if w.Code != http.StatusMethodNotAllowed {
  113. t.Fatalf("expected status %d", http.StatusMethodNotAllowed)
  114. }
  115. })
  116. }
  117. func TestCompile(t *testing.T) {
  118. version := "2"
  119. body := `
  120. package main
  121. import "fmt"
  122. func main() {
  123. fmt.fmtPrintln("Hello, playground")
  124. }
  125. `
  126. responseJSON := `{"Errors":"","Events":[{"Message":"Hello, playground\n","Kind":"stdout","Delay":0}]}`
  127. server := httptest.NewServer(
  128. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  129. formVersion := r.FormValue("version")
  130. formBody := r.FormValue("body")
  131. if formVersion != version {
  132. t.Fatalf("expected version sent to play.golang.org to be: %s, was: %s", version, formVersion)
  133. }
  134. if formBody != body {
  135. t.Fatalf("expected body sent to play.golang.org to be: %s, was: %s", body, formBody)
  136. }
  137. w.Header().Set("Content-Type", "application/json")
  138. w.WriteHeader(200)
  139. fmt.Fprintln(w, responseJSON)
  140. }),
  141. )
  142. defer server.Close()
  143. defer func(old string) { playCompileURL = old }(playCompileURL)
  144. playCompileURL = server.URL
  145. do(t, "POST", "/compile", func(r *http.Request) {
  146. r.PostForm = url.Values{
  147. "version": []string{version},
  148. "body": []string{body},
  149. }
  150. w := httptest.NewRecorder()
  151. handlerFunc(serveCompile).ServeHTTP(w, r)
  152. if w.Code != http.StatusOK {
  153. t.Fatalf("expected status: %d, got: %d", http.StatusOK, w.Code)
  154. }
  155. contentType := w.Header().Get("Content-Type")
  156. if w.Header().Get("Content-Type") != "application/json" {
  157. t.Fatalf("unexpected Content-Type: %s", contentType)
  158. }
  159. if strings.TrimSpace(w.Body.String()) != responseJSON {
  160. t.Fatalf("unexpected response body: %s", w.Body)
  161. }
  162. })
  163. }
  164. func TestBot(t *testing.T) {
  165. do(t, "GET", "/bot.html", func(r *http.Request) {
  166. w := httptest.NewRecorder()
  167. handlerFunc(serveBot).ServeHTTP(w, r)
  168. if w.Code != http.StatusOK {
  169. t.Fatalf("expected status: %d, got: %d", http.StatusOK, w.Code)
  170. }
  171. if !strings.Contains(w.Body.String(), contactEmail) {
  172. t.Fatalf("expected body to contain %s", contactEmail)
  173. }
  174. })
  175. }
  176. func do(t *testing.T, method, path string, f func(*http.Request)) {
  177. i, err := aetest.NewInstance(nil)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. defer i.Close()
  182. r, err := i.NewRequest(method, path, nil)
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. f(r)
  187. }