Skip to content

Commit

Permalink
feat: support query the popular HTTP headers on UI
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Jul 11, 2023
1 parent 9cdb8ec commit b435809
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 118 deletions.
4 changes: 4 additions & 0 deletions .github/testing/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ items:
statusCode: 500
bodyFieldsExpect:
code: 2
- name: get-popular-headers
request:
api: /PopularHeaders
method: POST
47 changes: 44 additions & 3 deletions console/atest-ui/src/views/TestCase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,50 @@ function headerChange() {
const header = testCaseWithSuite.value.data.request.header
let lastItem = header[header.length - 1]
if (lastItem.key !== '') {
header.push({
testCaseWithSuite.value.data.request.header.push({
key: '',
value: ''
})
}
}
function expectedHeaderChange() {
const header = testCaseWithSuite.value.data.response.header
let lastItem = header[header.length - 1]
if (lastItem.key !== '') {
testCaseWithSuite.value.data.response.header.push({
key: '',
value: ''
})
}
}
const radio1 = ref('1')
const pupularHeaders = ref([] as Pair[])
const requestOptions = {
method: 'POST'
};
fetch('/server.Runner/PopularHeaders', requestOptions)
.then(response => response.json())
.then(e => {
pupularHeaders.value = e.data
})
const queryPupularHeaders = (queryString: string, cb: (arg: any) => void) => {
const results = queryString
? pupularHeaders.value.filter(createFilter(queryString))
: pupularHeaders.value
results.forEach(e => {
e.value = e.key
})
cb(results)
}
const createFilter = (queryString: string) => {
return (v: Pair) => {
return (
v.key.toLowerCase().indexOf(queryString.toLowerCase()) !== -1
)
}
}
</script>

<template>
Expand All @@ -306,7 +342,12 @@ const radio1 = ref('1')
<el-table :data="testCaseWithSuite.data.request.header" style="width: 100%">
<el-table-column label="Key" width="180">
<template #default="scope">
<el-input v-model="scope.row.key" placeholder="Key" @change="headerChange" />
<el-autocomplete
v-model="scope.row.key"
:fetch-suggestions="queryPupularHeaders"
placeholder="Key"
@change="headerChange"
/>
</template>
</el-table-column>
<el-table-column label="Value">
Expand Down Expand Up @@ -345,7 +386,7 @@ const radio1 = ref('1')
<el-table :data="testCaseWithSuite.data.response.header" style="width: 100%">
<el-table-column label="Key" width="180">
<template #default="scope">
<el-input v-model="scope.row.key" placeholder="Key" @change="headerChange" />
<el-input v-model="scope.row.key" placeholder="Key" @change="expectedHeaderChange" />
</template>
</el-table-column>
<el-table-column label="Value">
Expand Down
8 changes: 8 additions & 0 deletions pkg/server/data/headers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- key: User-Agent
value: ""
- key: Content-Type
value: application/json
- key: Accept-Language
value: en-US,en;q=0.5
- key: Authorization
value: "Bearer token"
15 changes: 15 additions & 0 deletions pkg/server/remote_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"regexp"
"strings"

_ "embed"

"github.com/linuxsuren/api-testing/pkg/render"
"github.com/linuxsuren/api-testing/pkg/runner"
"github.com/linuxsuren/api-testing/pkg/testing"
Expand Down Expand Up @@ -402,6 +404,19 @@ func (s *server) Sample(ctx context.Context, in *Empty) (reply *HelloReply, err
return
}

// PopularHeaders returns a list of popular headers
func (s *server) PopularHeaders(ctx context.Context, in *Empty) (pairs *Pairs, err error) {
pairs = &Pairs{
Data: []*Pair{},
}

err = yaml.Unmarshal([]byte(popularHeaders), &pairs.Data)
return
}

//go:embed data/headers.yaml
var popularHeaders string

func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (testcases []testing.TestCase) {
reg, matchErr := regexp.Compile(`(.*?\{\{.*\.\w*.*?\}\})`)
targetReg, targetErr := regexp.Compile(`\.\w*`)
Expand Down
31 changes: 21 additions & 10 deletions pkg/server/remote_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,14 @@ func TestMapInterToPair(t *testing.T) {

func TestUpdateTestCase(t *testing.T) {
t.Run("no suite found", func(t *testing.T) {
writer := atesting.NewFileWriter("")
server := NewRemoteServer(writer)
server := getRemoteServerInTempDir()
server.UpdateTestCase(context.TODO(), &TestCaseWithSuite{
Data: &TestCase{},
})
})

t.Run("no data", func(t *testing.T) {
writer := atesting.NewFileWriter("")
server := NewRemoteServer(writer)
server := getRemoteServerInTempDir()
_, err := server.UpdateTestCase(context.TODO(), &TestCaseWithSuite{})
assert.Error(t, err)
})
Expand Down Expand Up @@ -388,19 +386,17 @@ func TestListTestCase(t *testing.T) {

func TestRemoteServerSuite(t *testing.T) {
t.Run("Get suite not found", func(t *testing.T) {
writer := atesting.NewFileWriter("")
ctx := context.Background()
server := NewRemoteServer(writer)
server := getRemoteServerInTempDir()

suite, err := server.GetTestSuite(ctx, &TestSuiteIdentity{Name: "fake"})
assert.NoError(t, err)
assert.Nil(t, suite)
})

t.Run("Get existing suite", func(t *testing.T) {
writer := atesting.NewFileWriter(os.TempDir())
ctx := context.Background()
server := NewRemoteServer(writer)
server := getRemoteServerInTempDir()

// create a new suite
_, err := server.CreateTestSuite(ctx, &TestSuiteIdentity{Name: "fake"})
Expand All @@ -424,15 +420,30 @@ func TestRemoteServerSuite(t *testing.T) {
})

t.Run("Delete non-exist suite", func(t *testing.T) {
writer := atesting.NewFileWriter(os.TempDir())
ctx := context.Background()
server := NewRemoteServer(writer)
server := getRemoteServerInTempDir()

_, err := server.DeleteTestSuite(ctx, &TestSuiteIdentity{Name: "fake"})
assert.Error(t, err)
})
}

func TestPopularHeaders(t *testing.T) {
ctx := context.Background()
server := getRemoteServerInTempDir()

pairs, err := server.PopularHeaders(ctx, &Empty{})
if assert.NoError(t, err) {
assert.Equal(t, 4, len(pairs.Data))
}
}

func getRemoteServerInTempDir() (server RunnerServer) {
writer := atesting.NewFileWriter(os.TempDir())
server = NewRemoteServer(writer)
return
}

//go:embed testdata/simple.yaml
var simpleSuite string

Expand Down

0 comments on commit b435809

Please sign in to comment.