adds prettify body

This commit is contained in:
Fabricio 2018-07-22 15:14:27 -03:00
parent 77821a1247
commit 98a6ff0269
3 changed files with 35 additions and 11 deletions

View file

@ -28,7 +28,7 @@ func (items *Captures) Add(capture Capture) {
}
}
func (items *Captures) GetRefs(itemBaseUrl string) []CaptureRef {
func (items *Captures) ToReferences(itemBaseUrl string) []CaptureRef {
refs := make([]CaptureRef, len(*items))
for i, item := range *items {
refs[i] = CaptureRef{

View file

@ -39,7 +39,7 @@ const dashboardHTML = `
--b0E: #a626a4;
--b0F: #986801;
--e0C: #82aaff;
--e0D: #82aaff;
--e0E: #c792ea;
}
@ -76,7 +76,7 @@ const dashboardHTML = `
.list-item {
flex-shrink: 0;
padding: 1rem;
color: var(--e0C);
color: var(--e0D);
background: var(--b07);
cursor: pointer;
margin-bottom: 0.5rem;
@ -118,6 +118,15 @@ const dashboardHTML = `
.res-inner:before {
content: "RESPONSE";
}
.bt-pretty {
position: absolute;
right: .5rem;
top: 0.25rem;
font-size: .75em;
color: var(--e0E);
text-decoration: none;
}
</style>
</head>
<body>
@ -137,12 +146,14 @@ const dashboardHTML = `
<div class="req">
<div class="req-inner">
<pre>{{request}}</pre>
<a ng-show="canPrettifyRequestBody" ng-click="prettifyBody('request')" href="#" class="bt-pretty">prettify</a>
<pre>{{request}}</pre>
</div>
</div>
<div class="res">
<div class="res-inner">
<a ng-show="canPrettifyResponseBody" ng-click="prettifyBody('response')" href="#" class="bt-pretty">prettify</a>
<pre>{{response}}</pre>
</div>
</div>
@ -159,6 +170,8 @@ const dashboardHTML = `
$http.get(item.itemUrl).then(r => {
$scope.request = r.data.request;
$scope.response = r.data.response;
$scope.canPrettifyRequestBody = r.data.request.indexOf('Content-Type: application/json') != -1;
$scope.canPrettifyResponseBody = r.data.response.indexOf('Content-Type: application/json') != -1;
});
}
@ -171,6 +184,15 @@ const dashboardHTML = `
return $scope.selectedId == item.id;
}
$scope.prettifyBody = key => {
let regex = /.*\n([\{\[].*[\}\]]).*/gs;
let data = $scope[key];
let match = regex.exec(data);
let body = match[1];
let prettyBody = JSON.stringify(JSON.parse(body), null, ' ');
$scope[key] = data.replace(body, prettyBody);
}
let socket = io();
socket.on('connect', () => {
socket.on('captures', captures => {

16
main.go
View file

@ -84,16 +84,17 @@ func getSocketHandler() http.Handler {
}
func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
res, err := t.RoundTripper.RoundTrip(req)
if err != nil {
return nil, errors.New("uh oh | " + err.Error() + " | " + req.URL.String())
}
reqDump, err := DumpRequest(req)
if err != nil {
return nil, err
}
res, err := t.RoundTripper.RoundTrip(req)
if err != nil {
return nil, errors.New("uh oh | " + err.Error() + " | " + req.URL.String())
}
resDump, err := DumpResponse(res)
if err != nil {
return nil, err
@ -119,10 +120,11 @@ func DumpRequest(req *http.Request) ([]byte, error) {
func DumpResponse(res *http.Response) ([]byte, error) {
var originalBody bytes.Buffer
res.Body = ioutil.NopCloser(io.TeeReader(res.Body, &originalBody))
reader := io.TeeReader(res.Body, &originalBody)
if res.Header.Get("Content-Encoding") == "gzip" {
res.Body, _ = gzip.NewReader(res.Body)
reader, _ = gzip.NewReader(reader)
}
res.Body = ioutil.NopCloser(reader)
resDump, err := httputil.DumpResponse(res, true)
res.Body = ioutil.NopCloser(&originalBody)
return resDump, err
@ -132,5 +134,5 @@ func emit() {
if socket == nil {
return
}
socket.Emit("captures", captures.GetRefs(host+dashboardItemsPath))
socket.Emit("captures", captures.ToReferences(host+dashboardItemsPath))
}