Exemple #1
0
func getInfo(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	post, err := posts.GetPost(vars["Title"])
	if err != nil {
		WriteError(w, err, 400)
		return
	}
	info := (*post).Info()
	WriteJson(w, map[string]posts.PostInfo{"Info": info})
}
Exemple #2
0
func getPost(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	post, err := posts.GetPost(vars["Title"])
	if err != nil {
		if strings.Contains(err.Error(), "not found") {
			WriteError(w, err, 404)
			return
		}
	}
	WriteOutputError(w, map[string]*posts.Post{"Post": post}, err)
}
Exemple #3
0
func getParagraph(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	post, err := posts.GetPost(vars["Title"])
	if err != nil {
		WriteError(w, err, 400)
		return
	}
	i, err := strconv.Atoi(vars["id"])
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	if i >= len(post.Content) {
		WriteErrorString(w, fmt.Sprintf("%s does not have paragraph %d", vars["Title"], i), 400)
		return
	}

	paragraph := post.Content[i]
	WriteJson(w, map[string]string{"Paragraph": paragraph})
}
Exemple #4
0
func TestGetPost(t *testing.T) {
	baconPost, err := posts.GetPost("Bacon Ipsum")
	if err != nil {
		t.Error(err.Error())
	}
	if baconPost == nil {
		t.Error("Expected non nil Bacon Ipsum post")
	}

	if baconPost.Title != "Bacon Ipsum" {
		t.Error("Expected Title as Bacon Ipsum")
	}
	if baconPost.Ignore != false {
		t.Error("Expected Ignore as false")
	}
	if baconPost.Subtitle != "Better than that Lorem nonsense" {
		t.Error("Expected Subtitle as Better than that Lorem nonsense")
	}
	if baconPost.Author != "Mark Tai" {
		t.Error("Expected Author as Mark Tai")
	}
	if baconPost.Image != "img/bacon-bg.jpg" {
		t.Error("Expected Image as img/bacon-bg.jpg")
	}

	timeConstant := "2006-01-02T15:04:05-07:00"
	expectedTime, _ := time.Parse(timeConstant, "2000-07-27T07:32:00-08:00")
	if !baconPost.Created.Equal(expectedTime) {
		t.Error("Expected Created as 2000-07-27T07:32:00-08:00")
	}
	expectedTime, _ = time.Parse(timeConstant, "2000-05-27T07:32:00-08:00")
	if !baconPost.Modified.Equal(expectedTime) {
		t.Error("Expected Modified as 2000-05-27T07:32:00-08:00")
	}
	if len(baconPost.Content) != 1 {
		t.Error("Expected Content to have length 1")
	} else if baconPost.Content[0] != "Here is bacon text" {
		t.Error("Expected Content[0] as Here is bacon text")
	}

	bacon2Post, err := posts.GetPost("Bacon Ipsum2")
	if err != nil {
		t.Error(err.Error())
	}
	if bacon2Post == nil {
		t.Error("Expected non nil Bacon Ipsum2 post")
	}
	if len(bacon2Post.Content) != 2 {
		t.Error("Expected Content to have length 2")
	}
	if bacon2Post.Content[1] != "And another one!" {
		t.Error("Expected Content[1] as And another one!")
	}

	_, err = posts.GetPost("ignore")
	if err == nil {
		t.Error("Expected error getting ignored post")
	}

	_, err = posts.GetPost("bad")
	if err == nil {
		t.Error("Expected error getting bad post")
	}
}