Skip to content

Commit c0879c4

Browse files
authored
Truncate body upload limit (#67)
* typo * truncate body upload, small typo fix
1 parent d2d50fa commit c0879c4

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Diff for: checks/http.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,26 @@ func HttpTest(
104104
RequestHeaders: r.Header,
105105
StatusCode: resp.StatusCode,
106106
Headers: headers,
107-
BodyString: string(body),
107+
BodyString: truncateAndStringifyBody(body),
108108
Variables: variables,
109109
}
110110
}
111111
return responses, finalBaseURL
112112
}
113113

114+
// truncateAndStringifyBody
115+
// in some lessons we yeet the entire body up to the server, but we really shouldn't ever care
116+
// about more than 100,000 stringified characters of it, so this protects against giant bodies
117+
func truncateAndStringifyBody(body []byte) string {
118+
bodyString := string(body)
119+
const maxBodyLength = 100000
120+
if len(bodyString) > maxBodyLength {
121+
fmt.Printf("truncating from %v to %v", len(bodyString), maxBodyLength)
122+
bodyString = bodyString[:maxBodyLength]
123+
}
124+
return bodyString
125+
}
126+
114127
func parseVariables(body []byte, vardefs []api.ResponseVariable, variables map[string]string) error {
115128
for _, vardef := range vardefs {
116129
val, err := valFromJQPath(vardef.Path, string(body))

Diff for: render/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func prettyPrintHTTPTest(test api.HTTPTest, variables map[string]string) string
258258
}
259259
if test.BodyContains != nil {
260260
interpolated := checks.InterpolateVariables(*test.BodyContains, variables)
261-
return fmt.Sprintf("Expecting JSON body to contain: %s", interpolated)
261+
return fmt.Sprintf("Expecting body to contain: %s", interpolated)
262262
}
263263
if test.BodyContainsNone != nil {
264264
interpolated := checks.InterpolateVariables(*test.BodyContainsNone, variables)

0 commit comments

Comments
 (0)