2025-02-03 17:30:50 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
|
|
import (
|
2025-04-02 18:50:03 +00:00
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
2025-02-03 17:30:50 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2025-04-02 18:50:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-02-03 17:30:50 +00:00
|
|
|
)
|
|
|
|
|
|
2025-04-22 14:40:16 +00:00
|
|
|
func TestCreateDetectDocumentTextExpectation(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
2025-05-12 15:11:50 +00:00
|
|
|
server := &MockServer{
|
2025-04-22 14:40:16 +00:00
|
|
|
Client: ts.Client(),
|
|
|
|
|
External: Address(ts.URL),
|
|
|
|
|
}
|
|
|
|
|
expectation := CreateDetectDocumentTextExpectation(t, server, "HI")
|
|
|
|
|
blocks, ok := expectation.Response.Body.(map[string]interface{})["Blocks"].([]map[string]interface{})
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
assert.Equal(
|
|
|
|
|
t,
|
|
|
|
|
"PAGE",
|
|
|
|
|
blocks[0]["BlockType"],
|
|
|
|
|
)
|
|
|
|
|
relationship, ok := blocks[0]["Relationships"].([]map[string]interface{})
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
assert.Equal(
|
|
|
|
|
t,
|
|
|
|
|
"CHILD",
|
|
|
|
|
relationship[0]["Type"],
|
|
|
|
|
)
|
|
|
|
|
ids, ok := relationship[0]["Ids"].([]string)
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
assert.Equal(
|
|
|
|
|
t,
|
|
|
|
|
ids[0],
|
|
|
|
|
blocks[1]["Id"],
|
|
|
|
|
)
|
|
|
|
|
assert.Equal(
|
|
|
|
|
t,
|
|
|
|
|
"HI",
|
|
|
|
|
blocks[1]["Text"],
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
func TestWaitForMockEndpoint(t *testing.T) {
|
|
|
|
|
if testing.Short() {
|
2025-05-03 01:16:53 +00:00
|
|
|
t.SkipNow()
|
2025-04-02 18:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-23 00:20:01 +00:00
|
|
|
server := CreateMockServer(t)
|
2025-04-02 18:50:03 +00:00
|
|
|
|
|
|
|
|
body := strings.NewReader(`{"team":"hello"}`)
|
|
|
|
|
req, err := http.NewRequest("GET", string(server.External), body)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
req.Header.Add("Hidden", "here")
|
|
|
|
|
|
|
|
|
|
expectation := MockExpectation{
|
|
|
|
|
Request: MockRequest{
|
|
|
|
|
Method: "GET",
|
|
|
|
|
Path: "/",
|
|
|
|
|
Headers: MockHeaders{},
|
|
|
|
|
Query: MockQueries{},
|
|
|
|
|
Body: `{"team":"hello"}`,
|
|
|
|
|
},
|
|
|
|
|
Response: MockResponse{
|
|
|
|
|
Code: 200,
|
|
|
|
|
Headers: MockHeaders{},
|
|
|
|
|
Body: "byebye",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreateMockExpectation(t, server, expectation)
|
|
|
|
|
|
|
|
|
|
resp, err := server.Client.Do(req)
|
|
|
|
|
require.NoError(t, err)
|
2025-06-23 15:58:20 +00:00
|
|
|
defer resp.Body.Close()
|
2025-04-02 18:50:03 +00:00
|
|
|
|
|
|
|
|
respStr, err := io.ReadAll(resp.Body)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, []byte("byebye"), respStr)
|
|
|
|
|
|
|
|
|
|
request := WaitForMockEndpoint(t, server, expectation.Request)
|
|
|
|
|
assert.Equal(t, []string([]string{"here"}), request.Headers["Hidden"])
|
|
|
|
|
}
|