/
reqreplacement.go
61 lines (53 loc) · 1.69 KB
/
reqreplacement.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
package wstestlib
import (
"encoding/base64"
"fmt"
"os"
)
// Replaces anything in the request that needs replacing. At time of writing
// this only involves the ability to use $IDLOAD=name$, where we look up the
// value from the package global "savedItems"
func doReqReplacements(req string, savedItemLookup map[string]string) (string, error) {
reqResult := req
for {
defMap, pre, post, err := parseDefinitions(reqResult)
if err != nil {
return "", err
}
// Expecting ONE or NO defs
if len(defMap) == 0 {
// Nothing here, stop
break
} else if len(defMap) == 1 {
// Process this item
for key, value := range defMap {
if key == "IDLOAD" {
if len(value) <= 0 {
return "", fmt.Errorf("IDLOAD: Missing replacement id name")
}
if replaceWith, ok := savedItemLookup[value]; !ok {
return "", fmt.Errorf("IDLOAD: No replacement text named: %v for request message: %v", value, req)
} else {
// Found it, do the replacement!
reqResult = pre + replaceWith + post
}
} else if key == "FILEBYTES" {
if len(value) <= 0 {
return "", fmt.Errorf("FILEBYTES: Missing file path")
}
// Read the file specified, include the bytes in the field encoded as base64
data, err := os.ReadFile(value)
if err != nil {
return "", fmt.Errorf(`Failed to read file into message field: %v`, req)
}
reqResult = pre + base64.StdEncoding.EncodeToString(data) + post
} else {
return "", fmt.Errorf("Unknown definition used on request message: %v", key)
}
}
} else {
return "", fmt.Errorf("Unexpected number of definitions (%v) on request line: %v", len(defMap), reqResult)
}
}
return reqResult, nil
}