mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
make headers quasi-consistent (#660)
possible breakages: * `FN_HEADER` on cold are no longer `s/-/_/` -- this is so that cold functions can rebuild the headers as they were when they came in on the request (fdks, specifically), there's no guarantee that a reversal `s/_/-/` is the original header on the request. * app and route config no longer `s/-/_/` -- it seemed really weird to rewrite the users config vars on these. should just pass them exactly as is to env. * headers no longer contain the environment vars (previously, base config; app config, route config, `FN_PATH`, etc.), these are still available in the environment. this gets rid of a lot of the code around headers, specifically the stuff that shoved everything into headers when constructing a call to begin with. now we just store the headers separately and add a few things, like FN_CALL_ID to them, and build a separate 'config' now to store on the call. I thought 'config' was more aptly named, 'env' was confusing, though now 'config' is exactly what 'base_vars' was, which is only the things being put into the env. we weren't storing this field in the db, this doesn't break unless there are messages in a queue from another version, anyway, don't think we're there and don't expect any breakage for anybody with field name changes. this makes the configuration stuff pretty straight forward, there's just two separate buckets of things, and cold just needs to mash them together into the env, and otherwise hot containers just need to put 'config' in the env, and then hot format can shove 'headers' in however they'd like. this seems better than my last idea about making this easier but worse (RIP). this means: * headers no longer contain all vars, the set of base vars can only be found in the environment. * headers is only the headers from request + call_id, deadline, method, url * for cold, we simply add the headers to the environment, prepending `FN_HEADER_` to them, BUT NOT upper casing or `s/-/_/` * fixes issue where async hot functions would end up with `Fn_header_` prefixed headers * removes idea of 'base' vars and 'env'. this was a strange concept. now we just have 'config' which was base vars, and headers, which was base_env+headers; i.e. they are disjoint now. * casing for all headers will lean to be `My-Header` style, which should help with consistency. notable exceptions for cold only are FN_CALL_ID, FN_METHOD, and FN_REQUEST_URL -- this is simply to avoid breakage, in either hot format they appear as `Fn_call_id` still. * removes FN_PARAM stuff * updated doc with behavior weird things left: `Fn_call_id` e.g. isn't a correctly formatted http header, it should likely be `Fn-Call-Id` but I wanted to live to fight another day on this one, it would add some breakage. examples to be posted of each format below closes #329
This commit is contained in:
@@ -95,14 +95,9 @@ func TestCallConfigurationRequest(t *testing.T) {
|
||||
req.Header.Add("Content-Length", contentLength)
|
||||
req.Header.Add("FN_PATH", "thewrongroute") // ensures that this doesn't leak out, should be overwritten
|
||||
|
||||
// let's assume we got there params from the URL
|
||||
params := make(Params, 0, 2)
|
||||
params = append(params, Param{Key: "YOGURT", Value: "garlic"})
|
||||
params = append(params, Param{Key: "LEGUME", Value: "garbanzo"})
|
||||
|
||||
call, err := a.GetCall(
|
||||
WithWriter(w), // XXX (reed): order matters [for now]
|
||||
FromRequest(appName, path, req, params),
|
||||
FromRequest(appName, path, req),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -148,7 +143,7 @@ func TestCallConfigurationRequest(t *testing.T) {
|
||||
t.Fatal("GetCall FromRequest should not fill payload, got non-empty payload", model.Payload)
|
||||
}
|
||||
|
||||
expectedBase := map[string]string{
|
||||
expectedConfig := map[string]string{
|
||||
"FN_FORMAT": format,
|
||||
"FN_APP_NAME": appName,
|
||||
"FN_PATH": path,
|
||||
@@ -158,62 +153,29 @@ func TestCallConfigurationRequest(t *testing.T) {
|
||||
"ROUTE_VAR": "BAR",
|
||||
}
|
||||
|
||||
expectedEnv := make(map[string]string)
|
||||
for k, v := range expectedBase {
|
||||
expectedEnv[k] = v
|
||||
}
|
||||
|
||||
for k, v := range expectedBase {
|
||||
if v2 := model.BaseEnv[k]; v2 != v {
|
||||
t.Fatal("base var mismatch", k, v, v2, model.BaseEnv)
|
||||
for k, v := range expectedConfig {
|
||||
if v2 := model.Config[k]; v2 != v {
|
||||
t.Fatal("config mismatch", k, v, v2, model.Config)
|
||||
}
|
||||
delete(expectedBase, k)
|
||||
delete(expectedConfig, k)
|
||||
}
|
||||
|
||||
if len(expectedBase) > 0 {
|
||||
t.Fatal("got extra vars in base env set, add me to tests ;)", expectedBase)
|
||||
if len(expectedConfig) > 0 {
|
||||
t.Fatal("got extra vars in config set, add me to tests ;)", expectedConfig)
|
||||
}
|
||||
|
||||
expectedEnv["FN_CALL_ID"] = model.ID
|
||||
expectedEnv["FN_METHOD"] = method
|
||||
expectedEnv["FN_REQUEST_URL"] = url
|
||||
|
||||
// do this before the "real" headers get sucked in cuz they are formatted differently
|
||||
expectedHeaders := make(http.Header)
|
||||
for k, v := range expectedEnv {
|
||||
expectedHeaders.Add(k, v)
|
||||
}
|
||||
|
||||
// from the request headers (look different in env than in req.Header, idk, up to user anger)
|
||||
// req headers down cases things
|
||||
expectedEnv["FN_HEADER_Myrealheader"] = "FOOLORD, FOOPEASANT"
|
||||
expectedEnv["FN_HEADER_Content_Length"] = contentLength
|
||||
|
||||
for k, v := range expectedEnv {
|
||||
if v2 := model.EnvVars[k]; v2 != v {
|
||||
t.Fatal("env var mismatch", k, v, v2, model.EnvVars)
|
||||
}
|
||||
delete(expectedEnv, k)
|
||||
}
|
||||
|
||||
if len(expectedEnv) > 0 {
|
||||
t.Fatal("got extra vars in base env set, add me to tests ;)", expectedBase)
|
||||
}
|
||||
expectedHeaders.Add("FN_CALL_ID", model.ID)
|
||||
expectedHeaders.Add("FN_METHOD", method)
|
||||
expectedHeaders.Add("FN_REQUEST_URL", url)
|
||||
|
||||
expectedHeaders.Add("MYREALHEADER", "FOOLORD")
|
||||
expectedHeaders.Add("MYREALHEADER", "FOOPEASANT")
|
||||
expectedHeaders.Add("Content-Length", contentLength)
|
||||
|
||||
checkExpectedHeaders(t, expectedHeaders, req.Header)
|
||||
|
||||
if w.Header()["Fn_call_id"][0] != model.ID {
|
||||
t.Fatal("response writer should have the call id, or else")
|
||||
}
|
||||
checkExpectedHeaders(t, expectedHeaders, model.Headers)
|
||||
|
||||
// TODO check response writer for route headers
|
||||
|
||||
// TODO idk what param even is or how to get them, but need to test those
|
||||
// TODO we should double check the things we're rewriting defaults of, like type, format, timeout, idle_timeout
|
||||
}
|
||||
|
||||
func TestCallConfigurationModel(t *testing.T) {
|
||||
@@ -228,7 +190,7 @@ func TestCallConfigurationModel(t *testing.T) {
|
||||
payload := "payload"
|
||||
typ := "sync"
|
||||
format := "default"
|
||||
env := map[string]string{
|
||||
cfg := models.Config{
|
||||
"FN_FORMAT": format,
|
||||
"FN_APP_NAME": appName,
|
||||
"FN_PATH": path,
|
||||
@@ -236,12 +198,10 @@ func TestCallConfigurationModel(t *testing.T) {
|
||||
"FN_TYPE": typ,
|
||||
"APP_VAR": "FOO",
|
||||
"ROUTE_VAR": "BAR",
|
||||
"DOUBLE_VAR": "BIZ, BAZ",
|
||||
}
|
||||
|
||||
cm := &models.Call{
|
||||
BaseEnv: env,
|
||||
EnvVars: env,
|
||||
Config: cfg,
|
||||
AppName: appName,
|
||||
Path: path,
|
||||
Image: image,
|
||||
@@ -266,18 +226,8 @@ func TestCallConfigurationModel(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// make sure headers seem reasonable
|
||||
req := callI.(*call).req
|
||||
|
||||
// NOTE these are added as is atm, and if the env vars were comma joined
|
||||
// they are not again here comma separated.
|
||||
expectedHeaders := make(http.Header)
|
||||
for k, v := range env {
|
||||
expectedHeaders.Add(k, v)
|
||||
}
|
||||
|
||||
checkExpectedHeaders(t, expectedHeaders, req.Header)
|
||||
|
||||
var b bytes.Buffer
|
||||
io.Copy(&b, req.Body)
|
||||
|
||||
@@ -300,7 +250,7 @@ func TestAsyncCallHeaders(t *testing.T) {
|
||||
format := "http"
|
||||
contentType := "suberb_type"
|
||||
contentLength := strconv.FormatInt(int64(len(payload)), 10)
|
||||
env := map[string]string{
|
||||
config := map[string]string{
|
||||
"FN_FORMAT": format,
|
||||
"FN_APP_NAME": appName,
|
||||
"FN_PATH": path,
|
||||
@@ -309,14 +259,16 @@ func TestAsyncCallHeaders(t *testing.T) {
|
||||
"APP_VAR": "FOO",
|
||||
"ROUTE_VAR": "BAR",
|
||||
"DOUBLE_VAR": "BIZ, BAZ",
|
||||
}
|
||||
headers := map[string][]string{
|
||||
// FromRequest would insert these from original HTTP request
|
||||
"Fn_header_content_type": contentType,
|
||||
"Fn_header_content_length": contentLength,
|
||||
"Content-Type": []string{contentType},
|
||||
"Content-Length": []string{contentLength},
|
||||
}
|
||||
|
||||
cm := &models.Call{
|
||||
BaseEnv: env,
|
||||
EnvVars: env,
|
||||
Config: config,
|
||||
Headers: headers,
|
||||
AppName: appName,
|
||||
Path: path,
|
||||
Image: image,
|
||||
@@ -344,14 +296,8 @@ func TestAsyncCallHeaders(t *testing.T) {
|
||||
// make sure headers seem reasonable
|
||||
req := callI.(*call).req
|
||||
|
||||
// NOTE these are added as is atm, and if the env vars were comma joined
|
||||
// they are not again here comma separated.
|
||||
expectedHeaders := make(http.Header)
|
||||
for k, v := range env {
|
||||
expectedHeaders.Add(k, v)
|
||||
}
|
||||
|
||||
// These should be here based on payload length and/or fn_header_* original headers
|
||||
expectedHeaders := make(http.Header)
|
||||
expectedHeaders.Set("Content-Type", contentType)
|
||||
expectedHeaders.Set("Content-Length", strconv.FormatInt(int64(len(payload)), 10))
|
||||
|
||||
@@ -402,7 +348,7 @@ func TestSubmitError(t *testing.T) {
|
||||
payload := "payload"
|
||||
typ := "sync"
|
||||
format := "default"
|
||||
env := map[string]string{
|
||||
config := map[string]string{
|
||||
"FN_FORMAT": format,
|
||||
"FN_APP_NAME": appName,
|
||||
"FN_PATH": path,
|
||||
@@ -414,8 +360,7 @@ func TestSubmitError(t *testing.T) {
|
||||
}
|
||||
|
||||
cm := &models.Call{
|
||||
BaseEnv: env,
|
||||
EnvVars: env,
|
||||
Config: config,
|
||||
AppName: appName,
|
||||
Path: path,
|
||||
Image: image,
|
||||
|
||||
Reference in New Issue
Block a user