File: method_verifier.go

package info (click to toggle)
golang-github-google-martian 3.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,324 kB
  • sloc: makefile: 5
file content (104 lines) | stat: -rw-r--r-- 2,670 bytes parent folder | download | duplicates (2)
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
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package method provides utilities for working with request methods.
package method

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/google/martian/v3"
	"github.com/google/martian/v3/parse"
	"github.com/google/martian/v3/verify"
)

type verifier struct {
	method string
	err    *martian.MultiError
}

type verifierJSON struct {
	Method string               `json:"method"`
	Scope  []parse.ModifierType `json:"scope"`
}

func init() {
	parse.Register("method.Verifier", verifierFromJSON)
}

// NewVerifier returns a new method verifier.
func NewVerifier(method string) (verify.RequestVerifier, error) {
	if method == "" {
		return nil, fmt.Errorf("%s is not a valid HTTP method", method)
	}
	return &verifier{
		method: method,
		err:    martian.NewMultiError(),
	}, nil
}

// ModifyRequest verifies that the request's method matches the given method
// in all modified requests. An error will be added to the contained *MultiError
// if a method is unmatched.
func (v *verifier) ModifyRequest(req *http.Request) error {
	m := req.Method

	if v.method != "" && v.method != m {
		err := fmt.Errorf("request(%v) method verification error: got %v, want %v", req.URL,
			v.method, m)
		v.err.Add(err)
	}

	return nil
}

// VerifyRequests returns an error if verification for any request failed.
// If an error is returned it will be of type *martian.MultiError.
func (v *verifier) VerifyRequests() error {
	if v.err.Empty() {
		return nil
	}

	return v.err
}

// ResetRequestVerifications clears all failed request verifications.
func (v *verifier) ResetRequestVerifications() {
	v.err = martian.NewMultiError()
}

// verifierFromJSON builds a method.Verifier from JSON.
//
// Example JSON:
// {
//   "method.Verifier": {
//     "scope": ["request"],
//     "method": "POST"
//   }
// }
func verifierFromJSON(b []byte) (*parse.Result, error) {

	msg := &verifierJSON{}

	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}
	v, err := NewVerifier(msg.Method)
	if err != nil {
		return nil, err
	}
	return parse.NewResult(v, msg.Scope)
}