Ejemplo n.º 1
0
func (h *Handler) StoreMessage(c web.C, w http.ResponseWriter, r *http.Request) {
	room := r.PostFormValue("room")
	if room == "" {
		fmt.Fprintf(w, response.New().WithError(errors.New("No room specified. Skipping.")).Json())
		return
	}

	msg := map[string]interface{}{
		"room":    html.EscapeString(r.PostFormValue("room")),
		"author":  html.EscapeString(r.PostFormValue("author")),
		"message": html.EscapeString(r.PostFormValue("message")),
		"at":      serializer.ParseJavaScriptTime(r.PostFormValue("time")),
	}

	logForReq(c, fmt.Sprintf("Inserting %s", msg))

	message, err := h.DB.InsertMessage(msg)

	if err != nil {
		errMsg := fmt.Sprintf("Could not insert message %s: %s", msg, err.Error())
		http.Error(w, response.New().WithError(errors.New(errMsg)).Json(), 500)
		return
	}
	fmt.Fprintf(w, response.New().WithMessage(message).Json())
}
Ejemplo n.º 2
0
func TestInsertMessageError(t *testing.T) {
	db := New()
	defer db.Close()

	msg := map[string]interface{}{
		"author":  "parker",
		"message": "the meeeessaaaaggeeee",
		"at":      serializer.ParseJavaScriptTime("Mon, 02 Jan 2006 15:04:05 UTC"),
	}

	actual, err := db.InsertMessage(msg)

	if err == nil {
		t.Fatal("InsertMessage() failed: expected error but got nil")
	}

	if actual != nil {
		t.Fatalf("InsertMessage() failed: expected nil, got %s", actual)
	}
}
Ejemplo n.º 3
0
func TestInsertMessage(t *testing.T) {
	db := New()
	defer db.Close()

	msg := map[string]interface{}{
		"room":    "#jekyll",
		"author":  "parker",
		"message": "the meeeessaaaaggeeee",
		"at":      serializer.ParseJavaScriptTime("Mon, 02 Jan 2006 15:04:05 MST"),
	}

	actual, err := db.InsertMessage(msg)

	if err != nil {
		t.Fatalf("InsertMessage() failed: encountered error '%s'", err)
	}

	if &actual == nil {
		t.Fatal("InsertMessage() failed: expected a Message, got nil")
	}

	if actual.Room != msg["room"] {
		t.Fatalf("InsertMessage() failed: expected .Room to be '%s', got '%s'", msg["room"], actual.Room)
	}

	if actual.Author != msg["author"] {
		t.Fatalf("InsertMessage() failed: expected .Author to be '%s', got '%s'", msg["author"], actual.Author)
	}

	if actual.Message != msg["message"] {
		t.Fatalf("InsertMessage() failed: expected .Message to be '%s', got '%s'", msg["message"], actual.Message)
	}

	expectedAt := "2006-01-02 15:04:05"
	if actual.At != expectedAt {
		t.Fatalf("InsertMessage() failed: expected .At to be '%s', got '%s'", expectedAt, actual.At)
	}
}