func ExampleSetupOAuth() { SetupOAuth("userkey", new(urlp), new(st), new(userp)) s := new(Stack) session := Sessions("123123123213", "mangoauth", &CookieOptions{Path: "/", MaxAge: 3600 * 24 * 7}) s.Middleware(session) m := http.NewServeMux() m.HandleFunc("/login", s.HandlerFunc(OAuthLogin)) m.HandleFunc("/logout", s.HandlerFunc(Logout)) m.HandleFunc("/oauthready", s.HandlerFunc(OAuthReady)) restrictedStack := new(Stack) restrictedStack.Middleware(session, MustLoggedIn) profile := func(env Env) (status Status, headers Headers, body Body) { return 200, Headers{}, Body("Hi, this is my profile") } m.HandleFunc("/profile", restrictedStack.HandlerFunc(profile)) fakelogin := func(env Env) (status Status, headers Headers, body Body) { s := env.Session() s["userkey"] = "user1" return 200, Headers{}, Body("") } m.HandleFunc("/fakelogin", s.HandlerFunc(fakelogin)) ts := httptest.NewServer(m) defer ts.Close() is := integrationtest.NewSession() is.Get(ts.URL + "/login") for k, _ := range storageImpl { fmt.Printf("have TemporaryCredential stored: %+v\n", strings.Contains(k, "TemporaryCredential")) } // logged in can access profile is.Get(ts.URL + "/fakelogin") storageImpl["User:user1"] = &user{"user1"} r, _ := is.Get(ts.URL + "/profile") b, _ := ioutil.ReadAll(r.Body) if strings.Contains(string(b), "Hi, this is my profile") { fmt.Println("can access profile when logged in") } // didn't log in can not access profile delete(storageImpl, "User:user1") r, _ = is.Get(ts.URL + "/profile") b, _ = ioutil.ReadAll(r.Body) if !strings.Contains(string(b), "Hi, this is my profile") { fmt.Println("yes, can not access profile if not logged in") } //Output: //have TemporaryCredential stored: true //can access profile when logged in //yes, can not access profile if not logged in }
func fetch(num_iid string, dimension bool) (imgs []*Image, err error) { s := integrationtest.NewSession() // r, err := http.Get(fmt.Sprintf("http://item.taobao.com/item.htm?id=%s", num_iid)) r1, err := s.Get(fmt.Sprintf("http://item.taobao.com/item.htm?id=%s", num_iid)) defer close(r1) if err != nil { if Verbose { log.Printf("taobaoimg: get taobao item page error: %s\n", err) } return } buf := bytes.NewBuffer([]byte{}) matchIndex := descUrlRegexp.FindReaderIndex(bufio.NewReader(io.TeeReader(r1.Body, buf))) if len(matchIndex) == 0 { return } descURL := buf.String()[matchIndex[0]:matchIndex[1]] r2, err := http.Get(descURL) if err != nil { if Verbose { log.Printf("taobaoimg: get taobao desc url %s error: %s\n", descURL, err) } return } b, _ := ioutil.ReadAll(r2.Body) defer close(r2) matches := imgTagRegexp.FindAllStringSubmatch(string(b), -1) for _, match := range matches { var img *Image if dimension { img, err = DecodeImage(match[1]) } else { img = &Image{ URL: match[1], } } if img != nil { imgs = append(imgs, img) } } return }
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) } }