diff --git a/src/app/spreed-webrtc-server/main.go b/src/app/spreed-webrtc-server/main.go index 5c6e8e52..60d8fe7a 100644 --- a/src/app/spreed-webrtc-server/main.go +++ b/src/app/spreed-webrtc-server/main.go @@ -25,7 +25,6 @@ import ( "bytes" "crypto/rand" "encoding/hex" - "encoding/json" "flag" "fmt" "html/template" @@ -209,17 +208,8 @@ func runner(runtime phoenix.Runtime) error { } // Load templates. - templateFuncMap := template.FuncMap{ - "json": func(obj interface{}) (template.JS, error) { - data, err := json.Marshal(obj) - if err != nil { - return "", err - } - return template.JS(data), nil - }, - } templates = template.New("") - templates.Delims("<%", "%>").Funcs(templateFuncMap) + templates.Delims("<%", "%>").Funcs(templateFuncMap()) // Load html templates folder err = filepath.Walk(path.Join(rootFolder, "html"), func(path string, info os.FileInfo, err error) error { diff --git a/src/app/spreed-webrtc-server/template.go b/src/app/spreed-webrtc-server/template.go new file mode 100644 index 00000000..de058ef3 --- /dev/null +++ b/src/app/spreed-webrtc-server/template.go @@ -0,0 +1,39 @@ +/* + * Spreed WebRTC. + * Copyright (C) 2013-2016 struktur AG + * + * This file is part of Spreed WebRTC. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package main + +import ( + "encoding/json" + "html/template" +) + +func templateFuncMap() template.FuncMap { + return template.FuncMap{ + "json": func(obj interface{}) (template.JS, error) { + data, err := json.Marshal(obj) + if err != nil { + return "", err + } + return template.JS(data), nil + }, + } +} diff --git a/src/app/spreed-webrtc-server/template_test.go b/src/app/spreed-webrtc-server/template_test.go new file mode 100644 index 00000000..3b34c95e --- /dev/null +++ b/src/app/spreed-webrtc-server/template_test.go @@ -0,0 +1,50 @@ +/* + * Spreed WebRTC. + * Copyright (C) 2013-2016 struktur AG + * + * This file is part of Spreed WebRTC. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package main + +import ( + "bytes" + "html/template" + "testing" +) + +const ( + templateString = `` + expectedString = `` +) + +type testPerson struct { + Name string `json:"name"` +} + +func TestHTMLTemplateWithJSON(t *testing.T) { + tmpl := template.New("").Funcs(templateFuncMap()) + if _, err := tmpl.Parse(templateString); err != nil { + t.Fatalf("Could not parse template '%s': %s", templateString, err.Error()) + } + buf := bytes.NewBuffer(nil) + tmpl.Execute(buf, testPerson{Name: "Peter"}) + out := buf.String() + if out != expectedString { + t.Fatalf("Strings do not match: got '%s', want '%s'", out, expectedString) + } +}