GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/http_method.cpp Lines: 51 68 75.0 %
Date: 2020-03-21 00:01:17 Branches: 35 70 50.0 %

Line Branch Exec Source
1
#include "expresscpp/http_method.hpp"
2
3
#include <cctype>
4
#include <string>
5
#include <string_view>
6
7
namespace expresscpp {
8
9
2265
HttpMethod beastVerbToHttpMethod(const boost::beast::http::verb beast_verb) {
10

2265
  switch (beast_verb) {
11
2249
    case boost::beast::http::verb::get:
12
2249
      return HttpMethod::Get;
13
11
    case boost::beast::http::verb::post:
14
11
      return HttpMethod::Post;
15
1
    case boost::beast::http::verb::delete_:
16
1
      return HttpMethod::Delete;
17
2
    case boost::beast::http::verb::patch:
18
2
      return HttpMethod::Patch;
19
2
    case boost::beast::http::verb::put:
20
2
      return HttpMethod::Put;
21
    default:
22
      return HttpMethod::All;
23
  }
24
}
25
26
7303
std::string getHttpMethodName(const HttpMethod method) {
27


7303
  switch (method) {
28
    case HttpMethod::All:
29
      return "ALL";
30
7196
    case HttpMethod::Get:
31
7196
      return "GET";
32
69
    case HttpMethod::Post:
33
69
      return "POST";
34
16
    case HttpMethod::Put:
35
16
      return "PUT";
36
3
    case HttpMethod::Delete:
37
3
      return "DELETE";
38
19
    case HttpMethod::Patch:
39
19
      return "PATCH";
40
    case HttpMethod::Head:
41
      return "HEAD";
42
    default:
43
      throw std::runtime_error("method not found " + std::to_string(static_cast<int>(method)));
44
  }
45
}
46
47
2265
boost::beast::http::verb getBeastVerbFromExpressVerb(const HttpMethod method) {
48


2265
  switch (method) {
49
    case HttpMethod::All:
50
      return boost::beast::http::verb::unknown;
51
2249
    case HttpMethod::Get:
52
2249
      return boost::beast::http::verb::get;
53
11
    case HttpMethod::Post:
54
11
      return boost::beast::http::verb::post;
55
2
    case HttpMethod::Put:
56
2
      return boost::beast::http::verb::put;
57
1
    case HttpMethod::Delete:
58
1
      return boost::beast::http::verb::delete_;
59
2
    case HttpMethod::Patch:
60
2
      return boost::beast::http::verb::patch;
61
    case HttpMethod::Head:
62
      return boost::beast::http::verb::head;
63
    default:
64
      throw std::runtime_error("method not found " + std::to_string(static_cast<int>(method)));
65
  }
66
}
67
68
6
HttpMethod getHttpMethodFromName(const std::string_view method) {
69
12
  std::string method_s = method.data();
70
6
  std::transform(method_s.begin(), method_s.end(), method_s.begin(), ::toupper);
71
6
  if (method_s == "ALL") {
72
1
    return HttpMethod::All;
73
5
  } else if (method_s == "GET") {
74
1
    return HttpMethod::Get;
75
4
  } else if (method_s == "POST") {
76
2
    return HttpMethod::Post;
77
2
  } else if (method_s == "PATCH") {
78
    return HttpMethod::Patch;
79
2
  } else if (method_s == "PUT") {
80
1
    return HttpMethod::Put;
81
1
  } else if (method_s == "DELETE") {
82
    return HttpMethod::Delete;
83
1
  } else if (method_s == "HEAD") {
84
    return HttpMethod::Head;
85
  }
86
87

1
  throw std::runtime_error(std::string("method not found ") + method.data());
88
}
89
90
}  // namespace expresscpp