Dragonfly2
68 строк · 1.9 Кб
1/*
2* Copyright 2020 The Dragonfly Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package transport
18
19import (
20"bytes"
21"context"
22"io"
23"net/http"
24"os"
25"testing"
26
27testifyassert "github.com/stretchr/testify/assert"
28"go.uber.org/mock/gomock"
29
30"d7y.io/dragonfly/v2/client/daemon/peer"
31"d7y.io/dragonfly/v2/client/daemon/test"
32)
33
34func TestTransport_RoundTrip(t *testing.T) {
35assert := testifyassert.New(t)
36ctrl := gomock.NewController(t)
37testData, err := os.ReadFile(test.File)
38assert.Nil(err, "load test file")
39
40var url = "http://x/y"
41peerTaskManager := peer.NewMockTaskManager(ctrl)
42peerTaskManager.EXPECT().StartStreamTask(gomock.Any(), gomock.Any()).DoAndReturn(
43func(ctx context.Context, req *peer.StreamTaskRequest) (io.ReadCloser, map[string]string, error) {
44assert.Equal(req.URL, url)
45return io.NopCloser(bytes.NewBuffer(testData)), nil, nil
46},
47)
48rt, _ := New(
49WithPeerIDGenerator(peer.NewPeerIDGenerator("127.0.0.1")),
50WithPeerTaskManager(peerTaskManager),
51WithCondition(func(r *http.Request) bool {
52return true
53}))
54assert.NotNil(rt)
55req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
56resp, err := rt.RoundTrip(req)
57assert.Nil(err)
58if err != nil {
59return
60}
61defer resp.Body.Close()
62output, err := io.ReadAll(resp.Body)
63assert.Nil(err)
64if err != nil {
65return
66}
67assert.Equal(testData, output)
68}
69