Go client for the Write.as API https://developers.write.as

post_test.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package writeas
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. )
  7. func TestCreatePost(t *testing.T) {
  8. wac := NewClient()
  9. p, err := wac.CreatePost(&PostParams{
  10. Title: "Title!",
  11. Content: "This is a post.",
  12. Font: "sans",
  13. })
  14. if err != nil {
  15. t.Errorf("Post create failed: %v", err)
  16. return
  17. }
  18. t.Logf("Post created: %+v", p)
  19. token := p.Token
  20. // Update post
  21. p, err = wac.UpdatePost(p.ID, token, &PostParams{
  22. Content: "Now it's been updated!",
  23. })
  24. if err != nil {
  25. t.Errorf("Post update failed: %v", err)
  26. return
  27. }
  28. t.Logf("Post updated: %+v", p)
  29. // Delete post
  30. err = wac.DeletePost(p.ID, token)
  31. if err != nil {
  32. t.Errorf("Post delete failed: %v", err)
  33. return
  34. }
  35. t.Logf("Post deleted!")
  36. }
  37. func TestGetPost(t *testing.T) {
  38. dwac := NewDevClient()
  39. res, err := dwac.GetPost("zekk5r9apum6p")
  40. if err != nil {
  41. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  42. } else {
  43. t.Logf("Post: %+v", res)
  44. if res.Content != "This is a post." {
  45. t.Errorf("Unexpected fetch results: %+v\n", res)
  46. }
  47. }
  48. wac := NewClient()
  49. res, err = wac.GetPost("3psnxyhqxy3hq")
  50. if err != nil {
  51. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  52. } else {
  53. if !strings.HasPrefix(res.Content, " Write.as Blog") {
  54. t.Errorf("Unexpected fetch results: %+v\n", res)
  55. }
  56. }
  57. }
  58. func TestPinPost(t *testing.T) {
  59. dwac := NewDevClient()
  60. _, err := dwac.LogIn("demo", "demo")
  61. if err != nil {
  62. t.Fatalf("Unable to log in: %v", err)
  63. }
  64. defer dwac.LogOut()
  65. err = dwac.PinPost("tester", &PinnedPostParams{ID: "olx6uk7064heqltf"})
  66. if err != nil {
  67. t.Fatalf("Pin failed: %v", err)
  68. }
  69. }
  70. func TestUnpinPost(t *testing.T) {
  71. dwac := NewDevClient()
  72. _, err := dwac.LogIn("demo", "demo")
  73. if err != nil {
  74. t.Fatalf("Unable to log in: %v", err)
  75. }
  76. defer dwac.LogOut()
  77. err = dwac.UnpinPost("tester", &PinnedPostParams{ID: "olx6uk7064heqltf"})
  78. if err != nil {
  79. t.Fatalf("Unpin failed: %v", err)
  80. }
  81. }
  82. func ExampleClient_CreatePost() {
  83. c := NewClient()
  84. // Publish a post
  85. p, err := c.CreatePost(&PostParams{
  86. Title: "Title!",
  87. Content: "This is a post.",
  88. Font: "sans",
  89. })
  90. if err != nil {
  91. fmt.Printf("Unable to create: %v", err)
  92. return
  93. }
  94. fmt.Printf("%s", p.Content)
  95. // Output: This is a post.
  96. }