feat: Support to generator JavaScript code from a HTTP testCase. (#400)
* feat: Support to generator JavaScript code from a HTTP testCase. * more friendly prompts in JavaScript code generator.
This commit is contained in:
parent
eb1973a486
commit
6f2fed9519
|
@ -69,6 +69,12 @@ func TestGenerators(t *testing.T) {
|
|||
assert.Equal(t, expectedPythonCode, result)
|
||||
})
|
||||
|
||||
t.Run("javascript", func(t *testing.T) {
|
||||
result, err := generator.GetCodeGenerator("JavaScript").Generate(nil, testcase)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedJavaScriptCode, result)
|
||||
})
|
||||
|
||||
formRequest := &atest.TestCase{Request: testcase.Request}
|
||||
formRequest.Request.Form = map[string]string{
|
||||
"key": "value",
|
||||
|
@ -91,6 +97,12 @@ func TestGenerators(t *testing.T) {
|
|||
assert.Equal(t, expectedFormRequestPythonCode, result, result)
|
||||
})
|
||||
|
||||
t.Run("javascript form HTTP request", func(t *testing.T) {
|
||||
result, err := generator.GetCodeGenerator("JavaScript").Generate(nil, formRequest)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedFormRequestJavaScriptCode, result)
|
||||
})
|
||||
|
||||
cookieRequest := &atest.TestCase{Request: formRequest.Request}
|
||||
cookieRequest.Request.Cookie = map[string]string{
|
||||
"name": "value",
|
||||
|
@ -113,6 +125,12 @@ func TestGenerators(t *testing.T) {
|
|||
assert.Equal(t, expectedCookieRequestPythonCode, result, result)
|
||||
})
|
||||
|
||||
t.Run("javascript cookie HTTP request", func(t *testing.T) {
|
||||
result, err := generator.GetCodeGenerator("JavaScript").Generate(nil, cookieRequest)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedCookieRequestJavaScriptCode, result, result)
|
||||
})
|
||||
|
||||
bodyRequest := &atest.TestCase{Request: testcase.Request}
|
||||
bodyRequest.Request.Body.Value = `{"key": "value"}`
|
||||
|
||||
|
@ -121,6 +139,12 @@ func TestGenerators(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedBodyRequestGoCode, result, result)
|
||||
})
|
||||
|
||||
t.Run("javascript body HTTP request", func(t *testing.T) {
|
||||
result, err := generator.GetCodeGenerator("JavaScript").Generate(nil, bodyRequest)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedBodyRequestJavaScriptCode, result, result)
|
||||
})
|
||||
}
|
||||
|
||||
//go:embed testdata/expected_go_code.txt
|
||||
|
@ -152,3 +176,15 @@ var expectedCookieRequestPythonCode string
|
|||
|
||||
//go:embed testdata/expected_go_body_request_code.txt
|
||||
var expectedBodyRequestGoCode string
|
||||
|
||||
//go:embed testdata/expected_javascript_code.txt
|
||||
var expectedJavaScriptCode string
|
||||
|
||||
//go:embed testdata/expected_javascript_form_request_code.txt
|
||||
var expectedFormRequestJavaScriptCode string
|
||||
|
||||
//go:embed testdata/expected_javascript_cookie_request_code.txt
|
||||
var expectedCookieRequestJavaScriptCode string
|
||||
|
||||
//go:embed testdata/expected_javascript_body_request_code.txt
|
||||
var expectedBodyRequestJavaScriptCode string
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
Copyright 2024 API Testing Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
async function makeRequest() {
|
||||
{{- if gt (len .Request.Header) 0 }}
|
||||
const headers = new Headers();
|
||||
{{- range $key, $val := .Request.Header}}
|
||||
headers.append("{{$key}}", "{{$val}}");
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
|
||||
{{- if gt (len .Request.Cookie) 0 }}
|
||||
const cookieHeader = [
|
||||
{{- range $key, $val := .Request.Cookie}}
|
||||
"{{$key}}={{$val}}",
|
||||
{{- end}}
|
||||
].join("; ");
|
||||
headers.append("Cookie", cookieHeader);
|
||||
{{- end}}
|
||||
|
||||
const requestOptions = {
|
||||
method: "{{.Request.Method}}",
|
||||
{{- if gt (len .Request.Header) 0 }}
|
||||
headers: headers,
|
||||
{{- end}}
|
||||
redirect: "follow"
|
||||
};
|
||||
|
||||
{{- if ne .Request.Body.String "" }}
|
||||
console.log('the body is ignored because this is a GET request')
|
||||
/*
|
||||
{{- else if gt (len .Request.Form) 0 }}
|
||||
console.log('the body is ignored because this is a GET request')
|
||||
/*
|
||||
{{- end}}
|
||||
{{- if gt (len .Request.Form) 0 }}
|
||||
const formData = new URLSearchParams();
|
||||
{{- range $key, $val := .Request.Form}}
|
||||
formData.append("{{$key}}", "{{$val}}");
|
||||
{{- end}}
|
||||
let body = formData;
|
||||
requestOptions.body = body;
|
||||
{{- else if ne .Request.Body.String ""}}
|
||||
let body = `{{.Request.Body.String | safeString}}`;
|
||||
requestOptions.body = body;
|
||||
{{- end}}
|
||||
{{- if ne .Request.Body.String "" }}
|
||||
*/
|
||||
{{- else if gt (len .Request.Form) 0 }}
|
||||
*/
|
||||
{{- end}}
|
||||
|
||||
try {
|
||||
const response = await fetch("{{.Request.API}}", requestOptions);
|
||||
if (response.ok) { // Check if HTTP status code is 200/OK
|
||||
const text = await response.text();
|
||||
console.log(text);
|
||||
} else {
|
||||
throw new Error('Network response was not ok. Status code: ' + response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fetch error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
makeRequest();
|
|
@ -46,10 +46,6 @@ func (g *golangGenerator) Generate(testSuite *testing.TestSuite, testcase *testi
|
|||
return
|
||||
}
|
||||
|
||||
func safeString(str string) template.HTML {
|
||||
return template.HTML(str)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterCodeGenerator("golang", NewGolangGenerator())
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
Copyright 2024 API Testing Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
package generator
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
)
|
||||
|
||||
func safeString(str string) template.HTML {
|
||||
return template.HTML(str)
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
Copyright 2024 API Testing Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
package generator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/linuxsuren/api-testing/pkg/testing"
|
||||
)
|
||||
|
||||
type javascriptGenerator struct {
|
||||
}
|
||||
|
||||
func NewJavaScriptGenerator() CodeGenerator {
|
||||
return &javascriptGenerator{}
|
||||
}
|
||||
|
||||
func (g *javascriptGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (result string, err error) {
|
||||
if testcase.Request.Method == "" {
|
||||
testcase.Request.Method = http.MethodGet
|
||||
}
|
||||
var tpl *template.Template
|
||||
if tpl, err = template.New("javascript template").Funcs(template.FuncMap{"safeString": safeString}).Parse(javascriptTemplate); err == nil {
|
||||
buf := new(bytes.Buffer)
|
||||
if err = tpl.Execute(buf, testcase); err == nil {
|
||||
result = buf.String()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterCodeGenerator("JavaScript", NewJavaScriptGenerator())
|
||||
}
|
||||
|
||||
//go:embed data/main.javascript.tpl
|
||||
var javascriptTemplate string
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
Copyright 2024 API Testing Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
async function makeRequest() {
|
||||
const headers = new Headers();
|
||||
headers.append("User-Agent", "atest");
|
||||
|
||||
const requestOptions = {
|
||||
method: "GET",
|
||||
headers: headers,
|
||||
redirect: "follow"
|
||||
};
|
||||
console.log('the body is ignored because this is a GET request')
|
||||
/*
|
||||
let body = `{"key": "value"}`;
|
||||
requestOptions.body = body;
|
||||
*/
|
||||
|
||||
try {
|
||||
const response = await fetch("https://www.baidu.com", requestOptions);
|
||||
if (response.ok) { // Check if HTTP status code is 200/OK
|
||||
const text = await response.text();
|
||||
console.log(text);
|
||||
} else {
|
||||
throw new Error('Network response was not ok. Status code: ' + response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fetch error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
makeRequest();
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
Copyright 2024 API Testing Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
async function makeRequest() {
|
||||
const headers = new Headers();
|
||||
headers.append("User-Agent", "atest");
|
||||
|
||||
const requestOptions = {
|
||||
method: "GET",
|
||||
headers: headers,
|
||||
redirect: "follow"
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch("https://www.baidu.com", requestOptions);
|
||||
if (response.ok) { // Check if HTTP status code is 200/OK
|
||||
const text = await response.text();
|
||||
console.log(text);
|
||||
} else {
|
||||
throw new Error('Network response was not ok. Status code: ' + response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fetch error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
makeRequest();
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
Copyright 2024 API Testing Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
async function makeRequest() {
|
||||
const headers = new Headers();
|
||||
headers.append("User-Agent", "atest");
|
||||
const cookieHeader = [
|
||||
"name=value",
|
||||
].join("; ");
|
||||
headers.append("Cookie", cookieHeader);
|
||||
|
||||
const requestOptions = {
|
||||
method: "GET",
|
||||
headers: headers,
|
||||
redirect: "follow"
|
||||
};
|
||||
console.log('the body is ignored because this is a GET request')
|
||||
/*
|
||||
const formData = new URLSearchParams();
|
||||
formData.append("key", "value");
|
||||
let body = formData;
|
||||
requestOptions.body = body;
|
||||
*/
|
||||
|
||||
try {
|
||||
const response = await fetch("https://www.baidu.com", requestOptions);
|
||||
if (response.ok) { // Check if HTTP status code is 200/OK
|
||||
const text = await response.text();
|
||||
console.log(text);
|
||||
} else {
|
||||
throw new Error('Network response was not ok. Status code: ' + response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fetch error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
makeRequest();
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
Copyright 2024 API Testing Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
async function makeRequest() {
|
||||
const headers = new Headers();
|
||||
headers.append("User-Agent", "atest");
|
||||
|
||||
const requestOptions = {
|
||||
method: "GET",
|
||||
headers: headers,
|
||||
redirect: "follow"
|
||||
};
|
||||
console.log('the body is ignored because this is a GET request')
|
||||
/*
|
||||
const formData = new URLSearchParams();
|
||||
formData.append("key", "value");
|
||||
let body = formData;
|
||||
requestOptions.body = body;
|
||||
*/
|
||||
|
||||
try {
|
||||
const response = await fetch("https://www.baidu.com", requestOptions);
|
||||
if (response.ok) { // Check if HTTP status code is 200/OK
|
||||
const text = await response.text();
|
||||
console.log(text);
|
||||
} else {
|
||||
throw new Error('Network response was not ok. Status code: ' + response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fetch error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
makeRequest();
|
|
@ -593,7 +593,7 @@ func TestCodeGenerator(t *testing.T) {
|
|||
t.Run("ListCodeGenerator", func(t *testing.T) {
|
||||
generators, err := server.ListCodeGenerator(ctx, &Empty{})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5, len(generators.Data))
|
||||
assert.Equal(t, 6, len(generators.Data))
|
||||
})
|
||||
|
||||
t.Run("GenerateCode, no generator found", func(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue