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.
 
 
 

136 lines
3.0 KiB

  1. // Copyright 2018 Google LLC
  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. // +build go1.8
  15. package httpreplay_test
  16. import (
  17. "bytes"
  18. "encoding/json"
  19. "io/ioutil"
  20. "net/http"
  21. "os"
  22. "testing"
  23. "time"
  24. "cloud.google.com/go/httpreplay"
  25. "cloud.google.com/go/internal/testutil"
  26. "cloud.google.com/go/storage"
  27. "golang.org/x/net/context"
  28. "google.golang.org/api/option"
  29. )
  30. func TestIntegration_RecordAndReplay(t *testing.T) {
  31. httpreplay.DebugHeaders()
  32. if testing.Short() {
  33. t.Skip("Integration tests skipped in short mode")
  34. }
  35. f, err := ioutil.TempFile("", "httpreplay")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. replayFilename := f.Name()
  40. if err := f.Close(); err != nil {
  41. t.Fatal(err)
  42. }
  43. defer os.Remove(replayFilename)
  44. projectID := testutil.ProjID()
  45. if projectID == "" {
  46. t.Skip("Need project ID. See CONTRIBUTING.md for details.")
  47. }
  48. ctx := context.Background()
  49. // Record.
  50. initial := time.Now()
  51. ibytes, err := json.Marshal(initial)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. rec, err := httpreplay.NewRecorder(replayFilename, ibytes)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. hc, err := rec.Client(ctx, option.WithTokenSource(
  60. testutil.TokenSource(ctx, storage.ScopeFullControl)))
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. wanta, wantc := run(t, hc)
  65. if err := rec.Close(); err != nil {
  66. t.Fatalf("rec.Close: %v", err)
  67. }
  68. // Replay.
  69. rep, err := httpreplay.NewReplayer(replayFilename)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. defer rep.Close()
  74. hc, err = rep.Client(ctx)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. gota, gotc := run(t, hc)
  79. if diff := testutil.Diff(gota, wanta); diff != "" {
  80. t.Error(diff)
  81. }
  82. if !bytes.Equal(gotc, wantc) {
  83. t.Errorf("got %q, want %q", gotc, wantc)
  84. }
  85. var gotInitial time.Time
  86. if err := json.Unmarshal(rep.Initial(), &gotInitial); err != nil {
  87. t.Fatal(err)
  88. }
  89. if !gotInitial.Equal(initial) {
  90. t.Errorf("initial: got %v, want %v", gotInitial, initial)
  91. }
  92. }
  93. // TODO(jba): test errors
  94. func run(t *testing.T, hc *http.Client) (*storage.BucketAttrs, []byte) {
  95. ctx := context.Background()
  96. client, err := storage.NewClient(ctx, option.WithHTTPClient(hc))
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. defer client.Close()
  101. b := client.Bucket(testutil.ProjID())
  102. attrs, err := b.Attrs(ctx)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. obj := b.Object("replay-test")
  107. w := obj.NewWriter(ctx)
  108. if _, err := w.Write([]byte{150, 151, 152}); err != nil {
  109. t.Fatal(err)
  110. }
  111. if err := w.Close(); err != nil {
  112. t.Fatal(err)
  113. }
  114. r, err := obj.NewReader(ctx)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. defer r.Close()
  119. contents, err := ioutil.ReadAll(r)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. return attrs, contents
  124. }