Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/httpjson: add minify_json template helper #74

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Added `minify_json` template helper function for minifying static JSON: [#x](https://github.com/elastic/stream/pull/x)

### Changed

### Fixed
Expand Down
19 changes: 15 additions & 4 deletions pkg/httpserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package httpserver

import (
"encoding/json"
"errors"
"os"
"strings"
"text/template"

ucfg "github.com/elastic/go-ucfg"
Expand Down Expand Up @@ -45,10 +47,11 @@ func (t *tpl) Unpack(in string) error {
parsed, err := template.New("").
Option("missingkey=zero").
Funcs(template.FuncMap{
"env": env,
"hostname": hostname,
"sum": sum,
"file": file,
"env": env,
"hostname": hostname,
"sum": sum,
"file": file,
"minify_json": minify,
}).
Parse(in)
if err != nil {
Expand Down Expand Up @@ -98,3 +101,11 @@ func file(path string) (string, error) {
}
return string(b), nil
}

func minify(body string) (string, error) {
var buf strings.Builder
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
err := enc.Encode(json.RawMessage(body))
return strings.TrimSpace(buf.String()), err
}
28 changes: 28 additions & 0 deletions pkg/httpserver/httpserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestHTTPServer(t *testing.T) {
status_code: 200
body: |-
{"next": "http://{{ hostname }}/page/{{ sum (.req_num) 1 }}"}

- path: "/page/{pagenum:[0-9]}"
methods: ["POST"]

Expand All @@ -52,6 +53,19 @@ func TestHTTPServer(t *testing.T) {
body: "{{ .request.vars.pagenum }}"
headers:
content-type: ["text/plain"]

- path: "/static/minify"
methods: ["GET"]

responses:
- status_code: 200
body: |-
{{ minify_json ` + "`" + `
{
"key1": "value1",
"key2": "<value2>"
}
` + "`" + `}}
`

f, err := ioutil.TempFile("", "test")
Expand Down Expand Up @@ -136,6 +150,20 @@ func TestHTTPServer(t *testing.T) {

assert.Equal(t, []byte{}, body)
})

t.Run("minify static JSON", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://"+addr+"/static/minify", nil)
require.NoError(t, err)

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)

body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
resp.Body.Close()

assert.Equal(t, `{"key1":"value1","key2":"<value2>"}`, string(body))
})
}

func TestRunAsSequence(t *testing.T) {
Expand Down
Loading