Welcome to mirror list, hosted at ThFree Co, Russian Federation.

handlers.go « gitlabstub « test - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46cff8181730e9f8cb44adaa687bfab9d09fc940 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package gitlabstub

import (
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"log"
	"net/http"
	"os"
	"regexp"
	"time"
)

func defaultAPIHandler(delay time.Duration, pagesRoot string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		domain := r.URL.Query().Get("host")
		if domain == "127.0.0.1" {
			// shortcut for healthy checkup done by WaitUntilRequestSucceeds
			w.WriteHeader(http.StatusNoContent)
			return
		}
		// to test slow responses from the API
		if delay > 0 {
			time.Sleep(delay)
		}

		// check if predefined response exists
		if responses, ok := apiResponses[domain]; ok {
			if err := json.NewEncoder(w).Encode(responses.virtualDomain(pagesRoot)); err != nil {
				log.Fatalf("fail to encode response for domain %q: %v", domain, err)
			}
			return
		}

		// serve lookup from files
		lookupFromFile(domain, w)
	}
}

func defaultAuthHandler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodPost {
			w.WriteHeader(http.StatusMethodNotAllowed)
			return
		}

		err := json.NewEncoder(w).Encode(struct {
			AccessToken string `json:"access_token"`
		}{
			AccessToken: "abc",
		})

		if err != nil {
			log.Fatal(err)
		}
	}
}

func defaultUserHandler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Header.Get("Authorization") != "Bearer abc" {
			w.WriteHeader(http.StatusForbidden)
		} else {
			w.WriteHeader(http.StatusOK)
		}
	}
}

func lookupFromFile(domain string, w http.ResponseWriter) {
	fixture, err := os.Open("../../shared/lookups/" + domain + ".json")
	if errors.Is(err, fs.ErrNotExist) {
		w.WriteHeader(http.StatusNoContent)

		log.Printf("GitLab domain %s source stub served 204", domain)
		return
	}

	if err != nil {
		log.Fatal(err)
	}

	defer fixture.Close()

	if _, err = io.Copy(w, fixture); err != nil {
		log.Fatal(err)
	}

	log.Printf("GitLab domain %s source stub served lookup", domain)
}

func handleAccessControlArtifactRequests(w http.ResponseWriter, r *http.Request) {
	authorization := r.Header.Get("Authorization")

	switch {
	case regexp.MustCompile(`/api/v4/projects/group/private/jobs/\d+/artifacts/delayed_200.html`).MatchString(r.URL.Path):
		sleepIfAuthorized(authorization, w)
	case regexp.MustCompile(`/api/v4/projects/group/private/jobs/\d+/artifacts/404.html`).MatchString(r.URL.Path):
		w.WriteHeader(http.StatusNotFound)
	case regexp.MustCompile(`/api/v4/projects/group/private/jobs/\d+/artifacts/500.html`).MatchString(r.URL.Path):
		returnIfAuthorized(authorization, w, http.StatusInternalServerError)
	case regexp.MustCompile(`/api/v4/projects/group/private/jobs/\d+/artifacts/200.html`).MatchString(r.URL.Path):
		returnIfAuthorized(authorization, w, http.StatusOK)
	case regexp.MustCompile(`/api/v4/projects/group/subgroup/private/jobs/\d+/artifacts/200.html`).MatchString(r.URL.Path):
		returnIfAuthorized(authorization, w, http.StatusOK)
	default:
		log.Printf("Unexpected r.URL.RawPath: %q", r.URL.Path)
		w.Header().Set("Content-Type", "text/html; charset=utf-8")
		w.WriteHeader(http.StatusTeapot)
	}
}

func handleAccessControlRequests(w http.ResponseWriter, r *http.Request) {
	authorization := r.Header.Get("Authorization")

	switch {
	case regexp.MustCompile(`/api/v4/projects/1\d{3}/pages_access`).MatchString(r.URL.Path):
		returnIfAuthorized(authorization, w, http.StatusOK)
	case regexp.MustCompile(`/api/v4/projects/2\d{3}/pages_access`).MatchString(r.URL.Path):
		returnIfAuthorized(authorization, w, http.StatusUnauthorized)
	case regexp.MustCompile(`/api/v4/projects/3\d{3}/pages_access`).MatchString(r.URL.Path):
		returnIfAuthorized(authorization, w, http.StatusUnauthorized)
		fmt.Fprint(w, "{\"error\":\"invalid_token\"}")
	default:
		log.Printf("Unexpected r.URL.RawPath: %q", r.URL.Path)
		w.Header().Set("Content-Type", "text/html; charset=utf-8")
		w.WriteHeader(http.StatusTeapot)
	}
}

func returnIfAuthorized(authorization string, w http.ResponseWriter, status int) {
	if authorization != "" {
		checkAuth(authorization)
		w.WriteHeader(status)
	} else {
		w.WriteHeader(http.StatusNotFound)
	}
}

func sleepIfAuthorized(authorization string, w http.ResponseWriter) {
	if authorization != "" {
		checkAuth(authorization)
		time.Sleep(2 * time.Second)
	} else {
		w.WriteHeader(http.StatusNotFound)
	}
}

func checkAuth(authorization string) {
	if authorization != "Bearer abc" {
		log.Fatalf("expected bearer abc but go %s", authorization)
	}
}