コード例 #1
0
ファイル: pdfrouter_test.go プロジェクト: mrlauer/pdfmaker
func TestRouter(t *testing.T) {
	SetPaths(get_top_dir())

	db := SetupDB("apptestdb")
	defer db.DeleteAll()
	r := MakeRouter()
	server := httptest.NewServer(r)
	defer server.Close()
	base := server.URL
	test_get(t, base+"/document/3/", http.StatusNotFound)
	test_get(t, base, http.StatusOK)

	// Test putting and getting
	doc := document.DefaultDocument()
	text := "This is some text БДЖ"
	doc.Text = text
	fontsz, _ := document.LengthFromString("13pt")
	doc.FontSize = fontsz
	jsonRep, err := json.Marshal(doc)
	if err != nil {
		t.Errorf("Could not marshal json")
	}
	req, err := http.NewRequest("POST", base+"/document/", bytes.NewReader(jsonRep))
	if err != nil {
		t.Errorf("Could not create request")
	}
	body := do_request(t, req, http.StatusOK)
	var doc2 document.Document
	err = json.Unmarshal(body, &doc2)
	if err != nil {
		t.Errorf("Could not unmarshall body")
	}
	if doc2.Text != doc.Text {
		t.Errorf("Returned document had wrong text %q", doc2.Text)
	}
	id := doc2.Id

	// Check that it's in the database
	{
		url := fmt.Sprintf("%s/document/%s/", base, id)
		body = test_get(t, url, http.StatusOK)
		var doc2 document.Document
		err = json.Unmarshal(body, &doc2)
		if err != nil {
			t.Errorf("Could not unmarshall body")
		}
		if doc2.Text != doc.Text {
			t.Errorf("Returned document had wrong text %q", doc2.Text)
		}
		if doc2.FontSize != doc.FontSize {
			t.Errorf("Returned document had wrong fontsize %s", doc2.FontSize)
		}
	}
}
コード例 #2
0
ファイル: pdfapp.go プロジェクト: mrlauer/pdfmaker
// editHandler is the handler for showing document edit pages
func editHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("%s %s\n", r.Method, r.URL.Path)
	fmt.Printf("%s\n", r.Header.Get("Accept"))
	templatePath := path.Join(TemplateDir, "main.html")
	contentPath := path.Join(TemplateDir, "content.html")
	templ, err := template.ParseFiles(templatePath, contentPath)
	header := w.Header()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	fonts := textproc.ListFontFamilies()
	sort.Strings(fonts)
	defaultDoc := document.DefaultDocument()
	defaultDocJSON, err := json.Marshal(defaultDoc)
	if err != nil {
		panic(err)
	}
	fontsJSON, err := json.Marshal(fonts)
	if err != nil {
		panic(err)
	}
	id := assignId(r)
	doc, err := DB.Fetch(id)
	if !id.IsNull() && err != nil {
		web.Error(w, err.Error(), http.StatusNotFound)
		return
	}
	docJSON, err := json.Marshal(doc)
	if err != nil {
		panic(err)
	}
	lengthREString := document.LengthREString()
	data := map[string]interface{}{"fonts": template.JS(fontsJSON),
		"doc":        template.JS(docJSON),
		"defaultDoc": template.JS(defaultDocJSON),
		"lengthRE":   lengthREString}
	header.Set("Content-Type", "text/html")
	err = templ.Execute(w, data)
	if err != nil {
		web.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}