func TestSubmitPostForm(t *testing.T) { setup() defer teardown() want := &thesrc.Post{ Title: "t", LinkURL: "http://example.com", Body: "b", } url_, _ := router.App().Get(router.SubmitPostForm).URL() url_.RawQuery = url.Values{"Title": []string{want.Title}, "url": []string{want.LinkURL}, "body": []string{want.Body}}.Encode() html, resp := getHTML(t, url_) if want := http.StatusOK; resp.Code != want { t.Errorf("got HTTP status %d, want %d", resp.Code, want) } if got, _ := html.Find("input[name=Title]").Attr("value"); got != want.Title { t.Errorf("got title %q, want %q", got, want.Title) } if got, _ := html.Find("input[name=LinkURL]").Attr("value"); got != want.LinkURL { t.Errorf("got link href %q, want %q", got, want.LinkURL) } if body := html.Find("textarea[name=Body]").Text(); body != want.Body { t.Errorf("got post body %q, want %q", body, want.Body) } }
func postCmd(args []string) { fs := flag.NewFlagSet("post", flag.ExitOnError) title := fs.String("title", "", "title of post") linkURL := fs.String("link", "", "link URL") body := fs.String("body", "", "body of post") fs.Usage = func() { fmt.Fprintln(os.Stderr, `usage: thesrc post [options] Submits a post. The options are: `) fs.PrintDefaults() os.Exit(1) } fs.Parse(args) if fs.NArg() != 0 { fs.Usage() } if *title == "" { log.Fatal(`Title must not be empty. See "thesrc post -h" for usage.`) } if *linkURL == "" { log.Fatal(`Link URL must not be empty. See "thesrc post -h" for usage.`) } post := &thesrc.Post{ Title: *title, LinkURL: *linkURL, Body: *body, } created, err := apiclient.Posts.Submit(post) if err != nil { log.Fatal(err) } if created { fmt.Print("created: ") } else { fmt.Print("exists: ") } url, err := router.App().Get(router.Post).URL("ID", strconv.Itoa(post.ID)) if err != nil { log.Fatal(err) } fmt.Println(baseURL.ResolveReference(url)) }
func TestSubmitPosts(t *testing.T) { setup() defer teardown() post := &thesrc.Post{ID: 0, Title: "t", LinkURL: "http://example.com", Body: "b"} var called bool APIClient = &thesrc.Client{ Posts: &thesrc.MockPostsService{ Submit_: func(post *thesrc.Post) (bool, error) { called = true post.ID = 1 return true, nil }, }, } v := url.Values{ "Title": []string{post.Title}, "LinkURL": []string{post.LinkURL}, "Body": []string{post.Body}, } url, _ := router.App().Get(router.SubmitPost).URL() req, err := http.NewRequest("POST", url.String(), strings.NewReader(v.Encode())) if err != nil { t.Fatal(err) } resp := httptest.NewRecorder() resp.Body = new(bytes.Buffer) testMux.ServeHTTP(resp, req) if want := http.StatusSeeOther; resp.Code != want { t.Errorf("got HTTP status %d, want %d", resp.Code, want) } if !called { t.Error("!called") } if loc, want := resp.Header().Get("location"), urlTo(router.Post, "ID", "1").String(); loc != want { t.Errorf("got Location %q, want %q", loc, want) } }
func TestPost(t *testing.T) { setup() defer teardown() post := &thesrc.Post{ID: 1, Title: "t", LinkURL: "http://example.com", Body: "b"} var called bool APIClient = &thesrc.Client{ Posts: &thesrc.MockPostsService{ Get_: func(id int) (*thesrc.Post, error) { if id != post.ID { t.Errorf("got post ID %v, want %v", id, post.ID) } called = true return post, nil }, }, } url, _ := router.App().Get(router.Post).URL("ID", strconv.Itoa(post.ID)) html, resp := getHTML(t, url) if want := http.StatusOK; resp.Code != want { t.Errorf("got HTTP status %d, want %d", resp.Code, want) } if !called { t.Error("!called") } a := html.Find("a.post-link") if a.Text() != post.Title { t.Errorf("got link text %q, want %q", a.Text(), post.Title) } if got, _ := a.Attr("href"); got != post.LinkURL { t.Errorf("got link href %q, want %q", got, post.LinkURL) } body := html.Find("p.post-body") if body.Text() != post.Body { t.Errorf("got post body %q, want %q", body.Text(), post.Body) } }
func TestPosts(t *testing.T) { setup() defer teardown() posts := []*thesrc.Post{{ID: 1, Title: "t", LinkURL: "http://example.com", Body: "b"}} var called bool APIClient = &thesrc.Client{ Posts: &thesrc.MockPostsService{ List_: func(opt *thesrc.PostListOptions) ([]*thesrc.Post, error) { called = true return posts, nil }, }, } url, _ := router.App().Get(router.Posts).URL() html, resp := getHTML(t, url) if want := http.StatusOK; resp.Code != want { t.Errorf("got HTTP status %d, want %d", resp.Code, want) } if !called { t.Error("!called") } for _, post := range posts { a := html.Find("a.post-link") if a.Text() != post.Title { t.Errorf("got link text %q, want %q", a.Text(), post.Title) } if got, _ := a.Attr("href"); got != post.LinkURL { t.Errorf("got link href %q, want %q", got, post.LinkURL) } body := html.Find("p.post-body") if body.Text() != post.Body { t.Errorf("got post body %q, want %q", body.Text(), post.Body) } } }
"sourcegraph.com/sourcegraph/thesrc" "sourcegraph.com/sourcegraph/thesrc/router" ) var ( // ReloadTemplates is whether to reload templates on each request. ReloadTemplates bool // StaticDir is the directory containing static assets. StaticDir = filepath.Join(defaultBase("sourcegraph.com/sourcegraph/thesrc/app"), "static") ) var ( APIClient = thesrc.NewClient(nil) schemaDecoder = schema.NewDecoder() appRouter = router.App() ) func Handler() *mux.Router { m := appRouter m.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(StaticDir)))) // TODO(sqs): add handlers for /favicon.ico and /robots.txt m.Get(router.Post).Handler(handler(servePost)) m.Get(router.Posts).Handler(handler(servePosts)) m.Get(router.SubmitPostForm).Handler(handler(serveSubmitPostForm)) m.Get(router.SubmitPost).Handler(handler(serveSubmitPost)) return m } type handler func(resp http.ResponseWriter, req *http.Request) error