File: protocol_test.go

package info (click to toggle)
golang-github-smira-go-ftp-protocol 0.0~git20140829.066b75c-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-backports, buster, buster-backports, forky, sid, trixie
  • size: 72 kB
  • sloc: makefile: 4
file content (94 lines) | stat: -rw-r--r-- 1,923 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package protocol

import (
	"io/ioutil"
	"net/http"
	"strings"
	"testing"
)

func TestProtocol(t *testing.T) {
	transport := &http.Transport{}
	transport.RegisterProtocol("ftp", &FTPRoundTripper{})

	client := &http.Client{Transport: transport}

	resp, err := client.Get("ftp://ftp.ru.debian.org/debian/README")
	if err != nil {
		t.Fatal(err)
	}

	if resp.StatusCode != 200 {
		t.Fatalf("resp.StatusCode 200 != %d", resp.StatusCode)
	}

	content, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}

	err = resp.Body.Close()
	if err != nil {
		t.Fatal(err)
	}

	if !strings.HasPrefix(string(content), "See http://www.debian.org/ for information about Debian GNU/Linux.") {
		t.Fatalf("unexpected content: %s", content)
	}

	resp, err = client.Get("ftp://ftp.ru.debian.org/debian/missing")
	if err != nil {
		t.Fatal(err)
	}

	if resp.StatusCode != 404 {
		t.Fatalf("resp.StatusCode 404 != %d", resp.StatusCode)
	}
}

func TestConcurrent(t *testing.T) {
	transport := &http.Transport{}
	transport.RegisterProtocol("ftp", &FTPRoundTripper{})

	client := &http.Client{Transport: transport}

	const concurrency = 4
	const count = 10

	done := make(chan struct{}, concurrency)

	for i := 0; i < concurrency; i++ {
		go func() {
			defer func() { done <- struct{}{} }()

			for j := 0; j < 10; j++ {
				resp, err := client.Get("ftp://ftp.ru.debian.org/debian/README")
				if err != nil {
					t.Fatal(err)
				}

				if resp.StatusCode != 200 {
					t.Fatalf("resp.StatusCode 200 != %d", resp.StatusCode)
				}

				content, err := ioutil.ReadAll(resp.Body)
				if err != nil {
					t.Fatal(err)
				}

				err = resp.Body.Close()
				if err != nil {
					t.Fatal(err)
				}

				if !strings.HasPrefix(string(content), "See http://www.debian.org/ for information about Debian GNU/Linux.") {
					t.Fatalf("unexpected content: %s", content)
				}
			}
		}()
	}

	for i := 0; i < concurrency; i++ {
		<-done
	}
}