forked from forkkit/web-embed-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformulas.go
160 lines (137 loc) · 5.43 KB
/
formulas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Formulas contains utilities for reading, using, and writing page formulas.
Reading and using usually happens in the host service.
Writing usually occurs when converting a colluder session capture to an initial page formula.
*/
package formulas
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)
var logger = log.New(os.Stdout, "[formulas] ", 0)
// Info about the serialized file structure
var FormulaInfoFileName = "formula.json"
var StaticDirName = "static"
var TemplateDirName = "template"
// The URL for rewritten absolute URLs hosted by the FormulaHost
var AbsoluteURLRoot = "/__wel_absolute/"
// The URL for the embedded script that is being tested
var EmbeddedScriptURL = "/__wel_embed.js"
// The script that contains the test probes
var ProbesURL = "/__wel_probes.js"
// The resources for the prober script that runs the tests
var ProberDistURL = "/__wel/prober/"
// THe URL for the script that runs the tests. The test scripts are separately loaded at ProbesURL.
var ProberURL = fmt.Sprintf("%vprober.js", ProberDistURL)
/*
PageFormula holds a description of a web page and its resources hosted locally and accessed by a web browser during an experiment.
*/
type PageFormula struct {
Comment string `json:"comment"` // A human readable description
TemplateData map[string]string `json:"template-data"` // data passed to the formula's go templates
Routes []Route `json:"routes"` // Determines what to do with incoming URL requests
InitialPath string `json:"initial-path"` // The URL path that the test runner should use for the main page of the formula
ProbeBasis ProbeBasis `json:"probe-basis"` // Expected values for test probes used to compare new embedded scripts
}
func (formula *PageFormula) JSON() ([]byte, error) {
return json.MarshalIndent(formula, "", "\t")
}
func NewPageFormula() *PageFormula {
return &PageFormula{
TemplateData: map[string]string{},
Routes: make([]Route, 0),
InitialPath: "/",
ProbeBasis: ProbeBasis{},
}
}
func ParsePageFormulaInfo(inputFile *os.File) (*PageFormula, error) {
formula := NewPageFormula()
data, err := ioutil.ReadAll(inputFile)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, formula)
if err != nil {
return nil, err
}
return formula, nil
}
// RouteType specifies how a Route will be used
type RouteType int
const (
TemplateRoute RouteType = iota // Routes to a go template
MockRoute // Routes to a JS class that acts like a web service
ServiceRoute // Routes to a service URL, locally hosted or remote
StaticRoute // Routes to a locally hosted static file
)
type Route struct {
ID string `json:"id"`
Path string `json:"path"` // A regex used route URLs
Type RouteType `json:"type"`
Value string `json:"value"`
Parameters map[string]string `json:"parameters"`
Headers map[string]string `json:"headers"` // HTTP headers to include in the response
}
func NewRoute(id string, path string, routeType RouteType, value string) *Route {
return &Route{
ID: id,
Path: path,
Type: routeType,
Value: value,
Parameters: make(map[string]string, 0),
Headers: make(map[string]string, 0),
}
}
/*
ProbeBasis holds expected values from test probes.
This information is usually used to check that future probes return similar values.
*/
type ProbeBasis map[string]interface{}
/**
GetInt64 looks for an int64 at basis[name] and if present returns its value and true (meaning found)
If basis[name] does not exist or is not an int64 then it returns defaultValue and false (meaning non-existent)
*/
func (basis ProbeBasis) GetInt64(name string, defaultValue int64) (int64, bool) {
val, ok := basis[name]
if ok == false {
return defaultValue, false
}
intVal, ok := val.(int64)
if ok == false {
return defaultValue, false
}
return intVal, true
}
/**
GetString looks for a string at basis[name] and if present returns its value and true (meaning found)
If basis[name] does not exist or is not a string then it returns defaultValue and false (meaning non-existent)
*/
func (basis ProbeBasis) GetString(name string, defaultValue string) (string, bool) {
val, ok := basis[name]
if ok == false {
return defaultValue, false
}
stringVal, ok := val.(string)
if ok == false {
return defaultValue, false
}
return stringVal, true
}
/*
Copyright 2019 FullStory, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/