func TestMakeClearUploader(t *testing.T) { mgodb.Setup("localhost", "tenpu_test") db := mgodb.NewDatabase("localhost", "tenpu_test") mgodb.CollectionDo(tenpu.CollectionName, func(c *mgo.Collection) { c.DropCollection() }) st := gridfs.NewStorage() http.HandleFunc("/upload_avatar", tenpu.MakeClearUploader("OwnerId", "posts", st)) http.HandleFunc("/load_avatar", tenpu.MakeFileLoader("id", st)) ts := httptest.NewServer(http.DefaultServeMux) defer ts.Close() //upload attachment repeatly req, _ := http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent)) req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj") res, err := http.DefaultClient.Do(req) req, _ = http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent)) req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj") res, err = http.DefaultClient.Do(req) req, _ = http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent)) req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj") res, err = http.DefaultClient.Do(req) if err != nil { panic(err) } b, _ := ioutil.ReadAll(res.Body) strb := string(b) if !strings.Contains(strb, "4facead362911fa23c000002") { t.Errorf("%+v", strb) } dbc := tenpu.DatabaseClient{Database: db} atts := dbc.Attachments("4facead362911fa23c000002") if len(atts) != 1 { t.Errorf("%+v", atts[0]) } res, err = http.Get(ts.URL + "/load_avatar?id=" + atts[0].Id) if err != nil { panic(err) } b, _ = ioutil.ReadAll(res.Body) strb = string(b) if strb != "the file content c\n" { t.Errorf("%+v", strb) } }
func main() { mangotemplate.AutoReload = true train.Config.SASS.DebugInfo = false mgodb.Setup(configs.DBUrl, configs.DatabaseName) global.ImageDatabase = mgodb.NewDatabase(configs.DBUrl, configs.ImageDatabaseName) mux := routes.Mux() log.Printf("Starting server on %s\n", configs.HttpPort) panic(http.ListenAndServe(configs.HttpPort, mux)) }
func TestUploader(t *testing.T) { mgodb.Setup("localhost", "tenpu_test") mgodb.CollectionDo(collectionName, func(c *mgo.Collection) { c.DropCollection() }) _, meta, _, _ := m.MakeForUpload(nil) http.HandleFunc("/postupload", tenpu.MakeUploader(m)) http.HandleFunc("/load", tenpu.MakeFileLoader(m)) ts := httptest.NewServer(http.DefaultServeMux) defer ts.Close() req, _ := http.NewRequest("POST", ts.URL+"/postupload", strings.NewReader(multipartContent)) req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj") res, err := http.DefaultClient.Do(req) if err != nil { panic(err) } b, _ := ioutil.ReadAll(res.Body) strb := string(b) if !strings.Contains(strb, "4facead362911fa23c000001") { t.Errorf("%+v", strb) return } atts := meta.Attachments("4facead362911fa23c000001") if len(atts) != 2 { t.Errorf("%+v", atts[0]) } res, err = http.Get(ts.URL + "/load?id=" + atts[0].Id) if err != nil { panic(err) } b, _ = ioutil.ReadAll(res.Body) strb = string(b) if strb != "the file content a\n" { t.Errorf("%+v", strb) } }
func TestUploadWithoutOwnerId(t *testing.T) { mgodb.Setup("localhost", "tenpu_test") http.HandleFunc("/errorpostupload", tenpu.MakeUploader("OwnerId", "posts", gridfs.NewStorage())) ts := httptest.NewServer(http.DefaultServeMux) defer ts.Close() req, _ := http.NewRequest("POST", ts.URL+"/errorpostupload", strings.NewReader(noOwnerIdPostContent)) req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj") res, err := http.DefaultClient.Do(req) if err != nil { panic(err) } b, _ := ioutil.ReadAll(res.Body) strb := string(b) if !strings.Contains(strb, "ownerId required") { t.Errorf("%+v", strb) } }
func TestIndexAndSearch(t *testing.T) { mgodb.Setup("localhost", "redisgosearch") client := redisgosearch.NewClient("localhost:6379", "theplant") e1 := &Entry{ Id: bson.ObjectIdHex("50344415ff3a8aa694000001"), GroupId: "Qortex", Title: "Thread Safety", Content: "The connection http://google.com Send and Flush methods cannot be called concurrently with other calls to these methods. The connection Receive method cannot be called concurrently with other calls to Receive. Because the connection Do method uses Send, Flush and Receive, the Do method cannot be called concurrently with Send, Flush, Receive or Do. Unless stated otherwise, all other concurrent access is allowed.", Attachments: []*Attachment{ { Filename: "QORTEX UI 0.88.pdf", ContentType: "application/pdf", CreatedAt: time.Now(), }, }, CreatedAt: time.Unix(10000, 0), } e2 := &Entry{ Id: bson.ObjectIdHex("50344415ff3a8aa694000002"), GroupId: "ASICS", Title: "redis is a client for the Redis database", Content: "The Conn interface is the primary interface for working with Redis. Applications create connections by calling the Dial, DialWithTimeout or NewConn functions. In the future, functions will be added for creating shareded and other types of connections.", Attachments: []*Attachment{ { Filename: "Screen Shot 2012-08-19 at 11.52.51 AM.png", ContentType: "image/png", CreatedAt: time.Now(), }, { Filename: "Alternate Qortex Logo.jpg", ContentType: "image/jpg", CreatedAt: time.Now(), }, }, CreatedAt: time.Unix(20000, 0), } mgodb.Save("entries", e1) client.Index(e1) mgodb.Save("entries", e2) client.Index(e2) var entries []*Entry count, err := client.Search("entries", "concurrent access", nil, 0, 10, &entries) if err != nil { t.Error(err) } if count != 1 { t.Error(entries) } if entries[0].Title != "Thread Safety" { t.Error(entries[0]) } var attachments []*IndexedAttachment _, err = client.Search("files", "alternate qortex", map[string]string{"group": "ASICS"}, 0, 20, &attachments) if err != nil { t.Error(err) } if attachments[0].Attachment.Filename != "Alternate Qortex Logo.jpg" || len(attachments) != 1 { t.Error(attachments[0]) } // sort var sorted []*Entry client.Search("entries", "other", nil, 0, 10, &sorted) if sorted[0].Id.Hex() != "50344415ff3a8aa694000002" { t.Error(sorted[0]) } }
func TestThumbnailLoader(t *testing.T) { mgodb.Setup("localhost", "tenpu_test") mgodb.DropCollections(collectionName, thumbnailsCollectionName) m := &maker{} _, meta, _, _ := m.MakeForUpload(nil) http.HandleFunc("/thumbpostupload", tenpu.MakeUploader(m)) http.HandleFunc("/thumbload", thumbnails.MakeLoader(&thumbnails.Configuration{ Maker: m, ThumbnailStorageMaker: &ThumbnailStorageMaker{}, ThumbnailSpecs: []*thumbnails.ThumbnailSpec{ {Name: "icon", Width: 100}, }, })) ts := httptest.NewServer(http.DefaultServeMux) defer ts.Close() var err error s := integrationtest.NewSession() res := integrationtest.Must(s.PostMultipart(ts.URL+"/thumbpostupload", func(w *multipart.Writer) { w.WriteField("OwnerId", "my12345") p1, err := w.CreateFormFile("t", "t.jpg") if err != nil { panic(err) } tf, err1 := os.Open("t.jpg") if err1 != nil { panic(err1) } defer tf.Close() io.Copy(p1, tf) })) b, _ := ioutil.ReadAll(res.Body) strb := string(b) if !strings.Contains(strb, "my12345") { t.Errorf("%+v", strb) } atts := meta.Attachments("my12345") if len(atts) != 1 { t.Errorf("%+v", atts) } res, err = http.Get(ts.URL + fmt.Sprintf("/thumbload?id=%s&thumb=icon", atts[0].Id)) if err != nil { panic(err) } f, err2 := os.OpenFile("thumbGenerated.jpg", os.O_CREATE|os.O_RDWR, 0666) if err2 != nil { panic(err2) } defer f.Close() io.Copy(f, res.Body) http.Get(ts.URL + fmt.Sprintf("/thumbload?id=%s&thumb=icon", atts[0].Id)) var thumbs []thumbnails.Thumbnail mgodb.FindAll(thumbnailsCollectionName, bson.M{}, &thumbs) if len(thumbs) != 1 { t.Errorf("%+v", thumbs) } }