OLD | NEW |
(Empty) | |
| 1 package frontend |
| 2 |
| 3 import ( |
| 4 "bytes" |
| 5 "io" |
| 6 "io/ioutil" |
| 7 "mime/multipart" |
| 8 "net/http" |
| 9 "net/http/httptest" |
| 10 "os" |
| 11 "path/filepath" |
| 12 "testing" |
| 13 |
| 14 "golang.org/x/net/context" |
| 15 |
| 16 "github.com/luci/gae/impl/memory" |
| 17 "github.com/luci/gae/service/datastore" |
| 18 "github.com/luci/luci-go/server/router" |
| 19 . "github.com/smartystreets/goconvey/convey" |
| 20 ) |
| 21 |
| 22 func withTestingContext(c *router.Context, next router.Handler) { |
| 23 ctx := memory.Use(context.Background()) |
| 24 ds := datastore.Get(ctx) |
| 25 testFileIdx, err := datastore.FindAndParseIndexYAML(filepath.Join("..",
"model", "testdata")) |
| 26 if err != nil { |
| 27 panic(err) |
| 28 } |
| 29 ds.Testable().AddIndexes(testFileIdx...) |
| 30 ds.Testable().CatchupIndexes() |
| 31 |
| 32 c.Context = ctx |
| 33 next(c) |
| 34 } |
| 35 |
| 36 func TestUpload(t *testing.T) { |
| 37 t.Parallel() |
| 38 |
| 39 r := router.New() |
| 40 mw := router.NewMiddlewareChain(withTestingContext) |
| 41 r.POST("/testfile/upload", mw.Extend(withParsedUploadForm), uploadHandle
r) |
| 42 srv := httptest.NewServer(r) |
| 43 client := &http.Client{} |
| 44 |
| 45 Convey("upload", t, func() { |
| 46 Convey("no matching aggregate file in datastore", func() { |
| 47 var buf bytes.Buffer |
| 48 multi := multipart.NewWriter(&buf) |
| 49 // Form files. |
| 50 f, err := os.Open(filepath.Join("testdata", "full_result
s_0.json")) |
| 51 So(err, ShouldBeNil) |
| 52 defer f.Close() |
| 53 multiFile, err := multi.CreateFormFile("file", "full_res
ults.json") |
| 54 So(err, ShouldBeNil) |
| 55 _, err = io.Copy(multiFile, f) |
| 56 So(err, ShouldBeNil) |
| 57 // Form fields. |
| 58 fields := []struct { |
| 59 key, val string |
| 60 }{ |
| 61 {"master", "chromium.chromiumos"}, |
| 62 {"builder", "test-builder"}, |
| 63 {"test_type", "test-type"}, |
| 64 } |
| 65 for _, field := range fields { |
| 66 f, err := multi.CreateFormField(field.key) |
| 67 So(err, ShouldBeNil) |
| 68 _, err = f.Write([]byte(field.val)) |
| 69 So(err, ShouldBeNil) |
| 70 } |
| 71 multi.Close() |
| 72 |
| 73 req, err := http.NewRequest("POST", srv.URL+"/testfile/u
pload", &buf) |
| 74 So(err, ShouldBeNil) |
| 75 req.Header.Set("Content-Type", multi.FormDataContentType
()) |
| 76 resp, err := client.Do(req) |
| 77 So(err, ShouldBeNil) |
| 78 defer resp.Body.Close() |
| 79 So(resp.StatusCode, ShouldEqual, http.StatusOK) |
| 80 |
| 81 b, err := ioutil.ReadAll(resp.Body) |
| 82 So(err, ShouldBeNil) |
| 83 So(string(b), ShouldEqual, "OK") |
| 84 }) |
| 85 }) |
| 86 } |
OLD | NEW |