From 2a3e8ab44b1509b19d577729bc5247d1ee8bba03 Mon Sep 17 00:00:00 2001 From: Philipp Hempel Date: Fri, 1 Nov 2024 15:01:31 +0100 Subject: [PATCH] renamed all functions with qjson to gjson; see #74055 --- README.md | 38 +++++++++++----------- api_testcase.go | 6 ++-- api_testcase_test.go | 4 +-- api_testsuite_test.go | 4 +-- pkg/lib/datastore/datastore.go | 4 +-- pkg/lib/datastore/datastore_test.go | 4 +-- pkg/lib/template/template_funcs.go | 6 ---- pkg/lib/template/template_funcs_test.go | 32 ------------------ pkg/lib/template/template_loader.go | 4 +-- pkg/lib/template/template_loader_test.go | 20 ++++++------ test/cookies/manifest.json | 2 +- test/datastore/check.json | 6 ++-- test/oauth2/store_client_token.json | 2 +- test/oauth2/store_code_token.json | 2 +- test/oauth2/store_password_token.json | 2 +- test/oauth2/store_token.json | 2 +- test/parallel/direct/parallel.json | 2 +- test/parallel/indirect/parallel_case.json | 2 +- test/proxy/write_to_proxies.json | 2 +- test/response/format/csv/csv_requests.json | 8 ++--- 20 files changed, 57 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index 98f4a5c6..2a8e2981 100644 --- a/README.md +++ b/README.md @@ -355,7 +355,7 @@ Manifest is loaded as **template**, so you can use variables, Go **range** and * }, // Store parts of the response into the datastore - "store_response_qjson": { + "store_response_gjson": { "eas_id": "body.0.eas._id", // Cookies are stored in `cookie` map @@ -770,13 +770,13 @@ To set data in custom store, you can use 4 methods: | | | | --- | --- | | `store` on the `manifest.json` top level | the data is set before the session authentication (if any) | -| `store_response_qjson` in `authentication.store_response_qjson` | | +| `store_response_gjson` in `authentication.store_response_gjson` | | | `store` on the **test** level | the data is set before **request** and **response** are evaluated | -| `store_response_qjson` on the test level | the data is set after each **response** (If you want the datestore to delete the current entry if no new one could be found with qjson. Just prepend the qjson key with a `!`. E.g. `"eventId":"!body.0._id"` will delete the `eventId` entry from the datastore if `body.0._id` could not be found in the response json) | +| `store_response_gjson` on the test level | the data is set after each **response** (If you want the datestore to delete the current entry if no new one could be found with gjson. Just prepend the gjson key with a `!`. E.g. `"eventId":"!body.0._id"` will delete the `eventId` entry from the datastore if `body.0._id` could not be found in the response json) | All methods use a Map as value, the keys of the map are **string**, the values can be anything. If the key (or **index**) ends in `[]` and Array is created if the key does not yet exist, or the value is appended to the Array if it does exist. -The method `store_response_qjson` takes only **string** as value. This qjson-string is used to parse the current response using the **qjson** feature. The return value from the qjson call is then stored in the datastore. +The method `store_response_gjson` takes only **string** as value. This gjson-string is used to parse the current response using the **gjson** feature. The return value from the gjson call is then stored in the datastore. ## Get Data from Custom Store @@ -790,7 +790,7 @@ If you access an invalid index for datastore `map[index]` or `slice[]` you get a To get the data from the sequential store an integer number has to be given to the datastore function as **string**. So `datastore "0"` would be a valid request. This would return the response from first test of the current manifest. `datastore "-1"` returns the last response from the current manifest. `datastore "-2"` returns second to last from the current manifest. If the index is wrong the function returns an error. -The sequential store stores the body and header of all responses. Use `qjson` to access values in the responses. See template functions [`datastore`](#datastore-key) and [`qjson`](#qjson-path-json). +The sequential store stores the body and header of all responses. Use `gjson` to access values in the responses. See template functions [`datastore`](#datastore-key) and [`gjson`](#gjson-path-json). When using relative indices (negative indices), use the same index to get values from the datastore to use in the request and response definition. Especially, for evaluating the current response, it has not yet been stored. So, `datastore "-1"` will still return the last response in the datastore. The current response will be appended after it was evaluated, and then will be returned with `datastore "-1"`. @@ -1929,13 +1929,13 @@ For `rows_to_map "column_a" "column_c" `: ## `datastore [key]` -Helper function to query the datastore; used most of the time in conjunction with `qjson`. +Helper function to query the datastore; used most of the time in conjunction with `gjson`. The `key`can be an int, or int64 accessing the store of previous responses. The responses are accessed in the order received. Using a negative value access the store from the back, so a value of **-2** would access the second to last response struct. This function returns a string, if the `key`does not exist, an empty string is returned. -If the `key` is a string, the datastore is accessed directly, allowing access to custom set values using `store` or `store_response_qjson`parameters. +If the `key` is a string, the datastore is accessed directly, allowing access to custom set values using `store` or `store_response_gjson`parameters. The datastore stores all responses in a list. We can retrieve the response (as a json string) by using this template function. `{{ datastore 0 }}` will render to @@ -1949,11 +1949,11 @@ The datastore stores all responses in a list. We can retrieve the response (as a } ``` -This function is intended to be used with the `qjson` template function. +This function is intended to be used with the `gjson` template function. The key `-` has a special meaning, it returns the entire custom datastore (not the sequentially stored responses) -## `qjson [path] [json]` +## `gjson [path] [json]` Helper function to extract fields from the `json` @@ -1968,7 +1968,7 @@ Helper function to extract fields from the `json` The call ```django -{{ qjson "foo.1.bar" "{\"foo": [{\"bar\": \"baz\"}, 42]}" }} +{{ gjson "foo.1.bar" "{\"foo": [{\"bar\": \"baz\"}, 42]}" }} ``` would return `baz`. @@ -1976,7 +1976,7 @@ would return `baz`. As an example with pipes, the call ```django -{{ datastore idx | qjson "header.foo.1" }} +{{ datastore idx | gjson "header.foo.1" }} ``` would return`bar` given the response above. @@ -1991,7 +1991,7 @@ Helper function to load a csv file | --- | --- | --- | | `@path` | `string` | A path to the csv file that should be loaded. The path is either relative to the manifest or a weburl | | `@delimiter` | `rune` | The delimiter that is used in the given csv e.g. `,` Defaults to `,` | -| `@result` | | The content of the csv as json array so we can work on this data with qjson | +| `@result` | | The content of the csv as json array so we can work on this data with gjson | The CSV **must** have a certain structur. If the structure of the given CSV differs, the apitest tool will fail with a error @@ -2037,7 +2037,7 @@ would result in As an example with pipes, the call ```django -{{ file_csv "some/path/example.csv" ',' | marshal | qjson "1.name" }} +{{ file_csv "some/path/example.csv" ',' | marshal | gjson "1.name" }} ``` would result in `martin` given the response above. @@ -2319,7 +2319,7 @@ would result in Helper function to return the result of an SQL statement from a sqlite3 file. - `@path`: string; a path to the sqlite file that should be loaded. The path is either relative to the manifest or a weburl - `@statement`: string; a SQL statement that returns data (`SELECT`) -- `@result`: the result of the statement as a json array so we can work on this data with qjson +- `@result`: the result of the statement as a json array so we can work on this data with gjson ### Example @@ -2491,7 +2491,7 @@ Example: ```django { "store": { - "access_token": {{ oauth2_password_token "my_client" "john" "pass" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_password_token "my_client" "john" "pass" | marshal | gjson "access_token" }} } } ``` @@ -2505,7 +2505,7 @@ Example: ```django { "store": { - "access_token": {{ oauth2_client_token "my_client" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_client_token "my_client" | marshal | gjson "access_token" }} } } ``` @@ -2521,7 +2521,7 @@ Example: ```django { "store": { - "access_token": {{ oauth2_code_token "my_client" "username" "myuser" "password" "mypass" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_code_token "my_client" "username" "myuser" "password" "mypass" | marshal | gjson "access_token" }} } } ``` @@ -2531,7 +2531,7 @@ Or: ```django { "store": { - "access_token": {{ oauth2_code_token "my_client" "guess_access" "true" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_code_token "my_client" "guess_access" "true" | marshal | gjson "access_token" }} } } ``` @@ -2547,7 +2547,7 @@ Example: ```django { "store": { - "access_token": {{ oauth2_password_token "my_client" "myuser" "mypass" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_password_token "my_client" "myuser" "mypass" | marshal | gjson "access_token" }} } } ``` diff --git a/api_testcase.go b/api_testcase.go index ea032a8e..fd8b288c 100644 --- a/api_testcase.go +++ b/api_testcase.go @@ -29,7 +29,7 @@ type Case struct { ResponseData any `json:"response"` ContinueOnFailure bool `json:"continue_on_failure"` Store map[string]any `json:"store"` // init datastore before testrun - StoreResponse map[string]string `json:"store_response_qjson"` // store qjson parsed response in datastore + StoreResponse map[string]string `json:"store_response_gjson"` // store gjson parsed response in datastore Timeout int `json:"timeout_ms"` WaitBefore *int `json:"wait_before_ms"` @@ -284,10 +284,10 @@ func (testCase Case) executeRequest(counter int) (responsesMatch compare.Compare } // Store in custom store - err = testCase.dataStore.SetWithQjson(apiRespJsonString, testCase.StoreResponse) + err = testCase.dataStore.SetWithGjson(apiRespJsonString, testCase.StoreResponse) if err != nil { testCase.LogReq(req) - err = fmt.Errorf("error store response with qjson: %s", err) + err = fmt.Errorf("error store response with gjson: %s", err) return responsesMatch, req, apiResp, err } diff --git a/api_testcase_test.go b/api_testcase_test.go index fdc893ef..e9b016c5 100644 --- a/api_testcase_test.go +++ b/api_testcase_test.go @@ -22,7 +22,7 @@ func init() { rand.Seed(time.Now().UnixNano()) } -func TestQjson(t *testing.T) { +func TestGjson(t *testing.T) { jsolo := `{"body":[{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":46,"global_object_id":"1@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":1,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:05+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":47,"global_object_id":"2@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":2,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:05+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":48,"global_object_id":"3@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":3,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:06+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":49,"global_object_id":"4@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":4,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:06+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"event":{"_id":50,"global_object_id":"1@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":1,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:06+01:00","type":"OBJECT_INDEX"}},{"event":{"_id":51,"global_object_id":"2@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":2,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:06+01:00","type":"OBJECT_INDEX"}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":52,"global_object_id":"5@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":5,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:06+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":53,"global_object_id":"6@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":6,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:06+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":54,"global_object_id":"7@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":7,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:06+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":55,"global_object_id":"8@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":8,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":56,"global_object_id":"9@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":9,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":57,"global_object_id":"10@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":10,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":58,"global_object_id":"11@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":11,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":59,"global_object_id":"12@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":12,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"event":{"_id":60,"global_object_id":"5@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":5,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INDEX"}},{"event":{"_id":61,"global_object_id":"6@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":6,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INDEX"}},{"event":{"_id":62,"global_object_id":"3@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":3,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INDEX"}},{"event":{"_id":63,"global_object_id":"4@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":4,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INDEX"}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":64,"global_object_id":"13@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":13,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:07+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":65,"global_object_id":"14@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":14,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:08+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":66,"global_object_id":"15@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":15,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:08+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"_session":{"token":"ac554a02-3ef0-42da-8ffb-603d73de95f9"},"event":{"_id":67,"global_object_id":"16@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":16,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","session_self":true,"timestamp":"2019-03-13T10:41:08+01:00","type":"OBJECT_INSERT"},"user":{"_generated_displayname":"Root","_id":1}},{"event":{"_id":68,"global_object_id":"8@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":8,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:08+01:00","type":"OBJECT_INDEX"}},{"event":{"_id":69,"global_object_id":"9@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":9,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:08+01:00","type":"OBJECT_INDEX"}},{"event":{"_id":70,"global_object_id":"7@ebe5e467-4da9-4cff-81b6-cee9b1385b7c","object_id":7,"object_version":1,"objecttype":"main","pollable":true,"schema":"USER","timestamp":"2019-03-13T10:41:08+01:00","type":"OBJECT_INDEX"}}],"header":{"Cache-Control":["no-cache"],"Content-Type":["application/json; charset=utf-8"],"Date":["Wed, 13 Mar 2019 09:41:16 GMT"],"Last-Modified":["Wed, 13 Mar 2019, 09:41:16 GMT"],"Pragma":["no-cache"],"Server":["Apache/2.4.25 (Debian)"],"Vary":["Origin,Accept-Encoding"],"X-Easydb-Api-Version":["1"],"X-Easydb-Base-Schema-Version":["207"],"X-Easydb-Solution":["simon"],"X-Easydb-User-Schema-Version":["2"]},"statuscode":200}` fmt.Println(gjson.Get(jsolo, "body|@reverse|0.event._id")) @@ -500,7 +500,7 @@ func TestCookieSetInDatastore(t *testing.T) { "status": "done" } }, - "store_response_qjson": { + "store_response_gjson": { "sess_cookie": "cookie.sess" } } diff --git a/api_testsuite_test.go b/api_testsuite_test.go index 4fd1b86e..192ec910 100644 --- a/api_testsuite_test.go +++ b/api_testsuite_test.go @@ -12,7 +12,7 @@ func TestLoadManifest(t *testing.T) { afero.WriteFile(filesystem.Fs, "externalFile", []byte(`{"load":{"me":"loaded"}}`), 644) - afero.WriteFile(filesystem.Fs, "testManifest.json", []byte(`{"testload": {{ file "externalFile" | qjson "load.me"}}}`), 644) + afero.WriteFile(filesystem.Fs, "testManifest.json", []byte(`{"testload": {{ file "externalFile" | gjson "load.me"}}}`), 644) s := Suite{manifestPath: "testManifest.json"} @@ -33,7 +33,7 @@ func TestLoadManifestCustomDelimiters(t *testing.T) { afero.WriteFile(filesystem.Fs, "testManifest.json", []byte(`// template-delims: ## ## // template-remove-tokens: "" "...." - {"testload": ## file "externalFile" | qjson "load.me" ##}"...."`), 0644) + {"testload": ## file "externalFile" | gjson "load.me" ##}"...."`), 0644) s := Suite{manifestPath: "testManifest.json"} res, err := s.loadManifest() diff --git a/pkg/lib/datastore/datastore.go b/pkg/lib/datastore/datastore.go index f7453e96..4da28098 100755 --- a/pkg/lib/datastore/datastore.go +++ b/pkg/lib/datastore/datastore.go @@ -52,8 +52,8 @@ func (data DatastoreIndexError) Error() string { return data.error } -// SetWithQjson stores the given response driven by a map key => qjson -func (ds *Datastore) SetWithQjson(jsonResponse string, storeResponse map[string]string) error { +// SetWithGjson stores the given response driven by a map key => gjson +func (ds *Datastore) SetWithGjson(jsonResponse string, storeResponse map[string]string) error { for k, qv := range storeResponse { setEmpty := false if len(qv) > 0 && qv[0] == '!' { diff --git a/pkg/lib/datastore/datastore_test.go b/pkg/lib/datastore/datastore_test.go index 272984f4..381030cd 100644 --- a/pkg/lib/datastore/datastore_test.go +++ b/pkg/lib/datastore/datastore_test.go @@ -38,7 +38,7 @@ func TestDataStore_GetSlice(t *testing.T) { func TestStoreTypeInt(t *testing.T) { store := NewStore(false) store.Set("ownInt", 1.0) - store.SetWithQjson(`{"id",1.000000}`, map[string]string{"jsonInt": "id"}) + store.SetWithGjson(`{"id",1.000000}`, map[string]string{"jsonInt": "id"}) oVal, _ := store.Get("ownInt") jVal, _ := store.Get("jsonInt") @@ -48,7 +48,7 @@ func TestStoreTypeInt(t *testing.T) { } store.Set("ownInt", 1.1) - store.SetWithQjson(`{"id",1.100000}`, map[string]string{"jsonInt": "id"}) + store.SetWithGjson(`{"id",1.100000}`, map[string]string{"jsonInt": "id"}) oVal, _ = store.Get("ownInt") jVal, _ = store.Get("jsonInt") diff --git a/pkg/lib/template/template_funcs.go b/pkg/lib/template/template_funcs.go index 62f4f4b6..e987fd3a 100644 --- a/pkg/lib/template/template_funcs.go +++ b/pkg/lib/template/template_funcs.go @@ -11,14 +11,8 @@ import ( "github.com/pkg/errors" "github.com/programmfabrik/apitest/pkg/lib/csv" "github.com/programmfabrik/apitest/pkg/lib/util" - "github.com/tidwall/gjson" ) -func qjson(path string, json string) string { - result := gjson.Get(json, path) - return result.Raw -} - // N returns a slice of n 0-sized elements, suitable for ranging over. (github.com/bradfitz) func N(n any) ([]struct{}, error) { switch v := n.(type) { diff --git a/pkg/lib/template/template_funcs_test.go b/pkg/lib/template/template_funcs_test.go index 21f45e2e..2e29ef72 100644 --- a/pkg/lib/template/template_funcs_test.go +++ b/pkg/lib/template/template_funcs_test.go @@ -5,42 +5,10 @@ import ( "reflect" "testing" - "github.com/programmfabrik/apitest/pkg/lib/test_utils" go_test_utils "github.com/programmfabrik/go-test-utils" "github.com/stretchr/testify/assert" ) -func Test_QJson_String(t *testing.T) { - json := `{ - "foo": "bar" - }` - go_test_utils.AssertStringEquals(t, qjson("foo", json), `"bar"`) -} - -func Test_QJson_Array(t *testing.T) { - json := `{ - "foo": [ - "bar", - 1 - ] - }` - test_utils.AssertJsonStringEquals(t, qjson("foo", json), `[ - "bar", - 1 - ]`) -} - -func Test_QJson_Object(t *testing.T) { - json := `{ - "foo": { - "bar": 1 - } - }` - test_utils.AssertJsonStringEquals(t, qjson("foo", json), `{ - "bar": 1 - }`) -} - func TestRowsToMap(t *testing.T) { tests := []struct { In []map[string]any diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index 4075314d..98813df8 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -97,7 +97,7 @@ func (loader *Loader) Render( } funcMap := template.FuncMap{ - "qjson": func(path string, json string) (result string, err error) { + "gjson": func(path string, json string) (result string, err error) { if json == "" { err = fmt.Errorf("The given json was empty") return @@ -105,7 +105,7 @@ func (loader *Loader) Render( result = gjson.Get(json, path).Raw if len(result) == 0 { - err = fmt.Errorf("'%s' was not found or was empty string. Qjson Input: %s", path, json) + err = fmt.Errorf("'%s' was not found or was empty string. Gjson Input: %s", path, json) } return }, diff --git a/pkg/lib/template/template_loader_test.go b/pkg/lib/template/template_loader_test.go index c1edfb08..fa6fb673 100644 --- a/pkg/lib/template/template_loader_test.go +++ b/pkg/lib/template/template_loader_test.go @@ -85,7 +85,7 @@ func TestBigIntRender(t *testing.T) { resp, _ := api.NewResponse(200, nil, nil, strings.NewReader(fmt.Sprintf(`{"bigINT":%s}`, inputNumber)), nil, api.ResponseFormat{}) respJson, _ := resp.ServerResponseToJsonString(false) - store.SetWithQjson(respJson, map[string]string{"testINT": "body.bigINT"}) + store.SetWithGjson(respJson, map[string]string{"testINT": "body.bigINT"}) res, err := loader.Render([]byte(`{{ datastore "testINT" }}`), "", nil) if err != nil { @@ -113,8 +113,8 @@ func TestRowsToMapTemplate(t *testing.T) { }) } -func TestRender_LoadFile_QJson_Params(t *testing.T) { - root := []byte(`{{ file_render "somefile.json" "foo" "bar" | qjson "key.1" }}`) +func TestRender_LoadFile_GJson_Params(t *testing.T) { + root := []byte(`{{ file_render "somefile.json" "foo" "bar" | gjson "key.1" }}`) target := []byte(`{ "key": ["{{ .Param1 }}", "{{ .Param2 }}"]}`) filesystem.Fs = afero.NewMemMapFs() @@ -274,10 +274,10 @@ int64,string,"string,array","int64,array" } } -func TestRender_LoadFile_CSVQjson(t *testing.T) { +func TestRender_LoadFile_CSV_Gjson(t *testing.T) { testCases := []struct { csv string - qjson string + gjson string expected string expectedErr error }{ @@ -291,7 +291,7 @@ int64,string,"string,array","int64,array" } for i, testCase := range testCases { t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) { - root := []byte(fmt.Sprintf(`{{ file_csv "somefile.json" ',' | marshal | qjson "%s" }}`, testCase.qjson)) + root := []byte(fmt.Sprintf(`{{ file_csv "somefile.json" ',' | marshal | gjson "%s" }}`, testCase.gjson)) target := []byte(testCase.csv) @@ -313,7 +313,7 @@ int64,string,"string,array","int64,array" } } -func TestRender_LoadFile_QJson(t *testing.T) { +func TestRender_LoadFile_GJson(t *testing.T) { testCases := []struct { path string json string @@ -381,7 +381,7 @@ func TestRender_LoadFile_QJson(t *testing.T) { } for i, testCase := range testCases { t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) { - root := []byte(fmt.Sprintf(`{{ file "somefile.json" | qjson "%s" }}`, testCase.path)) + root := []byte(fmt.Sprintf(`{{ file "somefile.json" | gjson "%s" }}`, testCase.path)) target := []byte(testCase.json) filesystem.Fs = afero.NewMemMapFs() @@ -403,7 +403,7 @@ func TestRender_LoadFile_QJson(t *testing.T) { } } -func Test_DataStore_QJson(t *testing.T) { +func Test_DataStore_GJson(t *testing.T) { response, _ := api.NewResponse( 200, map[string]any{"x-header": []string{"foo", "bar"}}, @@ -449,7 +449,7 @@ func Test_DataStore_QJson(t *testing.T) { for i, testCase := range testCases { t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) { - root := []byte(fmt.Sprintf(`{{ datastore 0 | qjson "%s" }}`, testCase.path)) + root := []byte(fmt.Sprintf(`{{ datastore 0 | gjson "%s" }}`, testCase.path)) res, err := loader.Render(root, "some/path", nil) go_test_utils.ExpectNoError(t, err, fmt.Sprintf("%s", err)) diff --git a/test/cookies/manifest.json b/test/cookies/manifest.json index 687d232c..6d725f19 100644 --- a/test/cookies/manifest.json +++ b/test/cookies/manifest.json @@ -58,7 +58,7 @@ }, "body": {} }, - "store_response_qjson": { + "store_response_gjson": { "sess_cookie": "cookie.sess" } }, diff --git a/test/datastore/check.json b/test/datastore/check.json index ee07cfae..0feef64c 100644 --- a/test/datastore/check.json +++ b/test/datastore/check.json @@ -9,9 +9,9 @@ "x-henk": "denk" }, "body": [ - {{ datastore -3 | qjson "body" }}, - {{ datastore -2 | qjson "body" }}, - {{ datastore -1 | qjson "body" }} + {{ datastore -3 | gjson "body" }}, + {{ datastore -2 | gjson "body" }}, + {{ datastore -1 | gjson "body" }} ] }, "response": { diff --git a/test/oauth2/store_client_token.json b/test/oauth2/store_client_token.json index 6ec2e564..2c3d87e0 100644 --- a/test/oauth2/store_client_token.json +++ b/test/oauth2/store_client_token.json @@ -1,6 +1,6 @@ { "name": "Store oauth2 client access token", "store": { - "access_token": {{ oauth2_client_token "my_client" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_client_token "my_client" | marshal | gjson "access_token" }} } } \ No newline at end of file diff --git a/test/oauth2/store_code_token.json b/test/oauth2/store_code_token.json index 620a6386..8d7d0978 100644 --- a/test/oauth2/store_code_token.json +++ b/test/oauth2/store_code_token.json @@ -1,6 +1,6 @@ { "name": "Store oauth2 code access token", "store": { - "access_token": {{ oauth2_code_token "my_client" "user" "password" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_code_token "my_client" "user" "password" | marshal | gjson "access_token" }} } } \ No newline at end of file diff --git a/test/oauth2/store_password_token.json b/test/oauth2/store_password_token.json index 4a37d53c..38bc9beb 100644 --- a/test/oauth2/store_password_token.json +++ b/test/oauth2/store_password_token.json @@ -1,6 +1,6 @@ { "name": "Store oauth2 password access token", "store": { - "access_token": {{ oauth2_password_token "my_client" "user" "password" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_password_token "my_client" "user" "password" | marshal | gjson "access_token" }} } } \ No newline at end of file diff --git a/test/oauth2/store_token.json b/test/oauth2/store_token.json index 19700fb1..608a733d 100644 --- a/test/oauth2/store_token.json +++ b/test/oauth2/store_token.json @@ -1,6 +1,6 @@ { "name": "Store oauth2 access token", "store": { - "access_token": {{ oauth2_implicit_token "my_client" "user" "password" | marshal | qjson "access_token" }} + "access_token": {{ oauth2_implicit_token "my_client" "user" "password" | marshal | gjson "access_token" }} } } \ No newline at end of file diff --git a/test/parallel/direct/parallel.json b/test/parallel/direct/parallel.json index 9097c081..f09e672d 100644 --- a/test/parallel/direct/parallel.json +++ b/test/parallel/direct/parallel.json @@ -17,7 +17,7 @@ } } }, - "store_response_qjson": { + "store_response_gjson": { "responses[]": "body.body.n" } } diff --git a/test/parallel/indirect/parallel_case.json b/test/parallel/indirect/parallel_case.json index 9a8ce7c6..0ede1bbe 100644 --- a/test/parallel/indirect/parallel_case.json +++ b/test/parallel/indirect/parallel_case.json @@ -16,7 +16,7 @@ } } }, - "store_response_qjson": { + "store_response_gjson": { "responses[]": "body.body.n" } } diff --git a/test/proxy/write_to_proxies.json b/test/proxy/write_to_proxies.json index df74a155..0c05d843 100644 --- a/test/proxy/write_to_proxies.json +++ b/test/proxy/write_to_proxies.json @@ -43,7 +43,7 @@ "offset": {{ datastore "max_entries" }} } }, - "store_response_qjson": { + "store_response_gjson": { "test_image_offset": "body.offset" } } diff --git a/test/response/format/csv/csv_requests.json b/test/response/format/csv/csv_requests.json index 3fdb4b92..1502d434 100644 --- a/test/response/format/csv/csv_requests.json +++ b/test/response/format/csv/csv_requests.json @@ -48,7 +48,7 @@ "response": { "statuscode": 200 }, - "store_response_qjson": { + "store_response_gjson": { "name": "body.0.name" }, "reverse_test_result": true @@ -66,7 +66,7 @@ "type": "csv" } }, - "store_response_qjson": { + "store_response_gjson": { "name": "body.0.name" } }, @@ -77,7 +77,7 @@ "endpoint": "dummy.csv", "method": "GET" }, - "store_response_qjson": { + "store_response_gjson": { "ext": "body.0.extension" }, "reverse_test_result": true @@ -94,7 +94,7 @@ "type": "csv" } }, - "store_response_qjson": { + "store_response_gjson": { "ext": "body.0.extension" } }