Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pretty printing for JSON, XML/HTML, CSS responses in the detailPanel #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ coverage
build/Release
node_modules
.lock-wscript
temp
temp
.vscode
17 changes: 9 additions & 8 deletions lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var zlib = require('zlib'),
events = require('events'),
iconv = require('iconv-lite'),
proxyUtil = require("./util"),
webUtil = require("./web_util"),
logUtil = require("./log");

//option.filename
Expand Down Expand Up @@ -140,24 +141,24 @@ function Recorder(option){
var record = doc[0],
resHeader = record['resHeader'] || {};
try{
var headerStr = JSON.stringify(resHeader),
charsetMatch = headerStr.match(/charset="?([a-zA-Z0-9\-]+)"?/),
imageMatch = resHeader && resHeader["content-type"];
var headerStr = JSON.stringify(resHeader),
charsetMatch = headerStr.match(/charset="?([a-zA-Z0-9\-]+)"?/),
contentTypeToMatch = resHeader && resHeader["content-type"],
responseType = webUtil.getResponseType(contentTypeToMatch);

if(charsetMatch && charsetMatch.length){

var currentCharset = charsetMatch[1].toLowerCase();
if(currentCharset != "utf-8" && iconv.encodingExists(currentCharset)){
bodyContent = iconv.decode(bodyContent, currentCharset);
}
result.type = "text";
result.type = responseType;
result.content = bodyContent.toString();
}else if(imageMatch && /image/i.test(imageMatch)){

}else if(contentTypeToMatch && /image/i.test(contentTypeToMatch)){
result.type = "image";
result.mime = imageMatch;
result.mime = contentTypeToMatch;
result.content = bodyContent;
}else{
result.type = responseType;
result.content = bodyContent.toString();
}
}catch(e){}
Expand Down
49 changes: 49 additions & 0 deletions lib/web_util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var JSON_CONTENT_TYPES = [
"application/json"
],
JAVASCRIPT_CONTENT_TYPES = [
"application/javascript",
"application/x-javascript"
],
XML_CONTENT_TYPES = [
"text/xml",
"application/xml",
"application/xcap-diff+xml",
"application/xenc+xml",
"application/patch-ops-error+xml",
"application/resource-lists+xml",
"application/rls-services+xml",
"application/resource-lists-diff+xml",
"application/xslt+xml"
],
HTML_CONTENT_TYPES = [
"text/html",
"application/xhtml+xml"
],
CSS_CONTENT_TYPES = [
"text/css"
];

module.exports.responseTypes = {
XML_TYPE : "xml",
HTML_TYPE : "html",
JSON_TYPE : "json",
JS_TYPE : "javascript",
CSS_TYPE : "css",
TEXT_TYPE : "text"
};

module.exports.getResponseType = function(contentType){
if (XML_CONTENT_TYPES.some(function (i){ return contentType.indexOf(i.toLowerCase()) != -1; }))
return this.responseTypes.XML_TYPE;
else if (JSON_CONTENT_TYPES.some(function (i){ return contentType.indexOf(i.toLowerCase()) != -1; }))
return this.responseTypes.JSON_TYPE;
else if (JAVASCRIPT_CONTENT_TYPES.some(function (i){ return contentType.indexOf(i.toLowerCase()) != -1; }))
return this.responseTypes.JS_TYPE;
else if (HTML_CONTENT_TYPES.some(function (i){ return contentType.indexOf(i.toLowerCase()) != -1; }))
return this.responseTypes.HTML_TYPE;
else if (CSS_CONTENT_TYPES.some(function (i){ return contentType.indexOf(i.toLowerCase()) != -1; }))
return this.responseTypes.CSS_TYPE;
else
return this.responseTypes.TEXT_TYPE;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"node-easy-cert": "^1.0.0",
"node-forge": "^0.6.39",
"npm": "^2.7.0",
"pretty-data": "^0.40.0",
"promise": "^7.0.4",
"qrcode-npm": "0.0.3",
"stream-throttle": "^0.1.3",
Expand Down
46 changes: 30 additions & 16 deletions web/build/detailPanel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var PrettyData = require("../../node_modules/pretty-data");
var Util = require("../../lib/web_util");

function init(React){

var DetailPanel = React.createClass({displayName: "DetailPanel",
Expand Down Expand Up @@ -25,6 +28,7 @@ function init(React){
self.setState({
body : {
body : resObj.content,
type: resObj.type,
id : resObj.id
}
});
Expand Down Expand Up @@ -72,37 +76,47 @@ function init(React){
var imgEl = { __html : '<img src="'+ this.state.body.img +'" />'};
bodyContent = (React.createElement("div", {dangerouslySetInnerHTML: imgEl}));
}else{
try{
// trying to prettify, sometimes content-types are not right and that raises exceptions
if (this.state.body.type == Util.responseTypes.JSON_TYPE)
this.state.body.body = PrettyData.pd.json(this.state.body.body);
else if(this.state.body.type == Util.responseTypes.XML_TYPE || this.state.body.type == Util.responseTypes.HTML_TYPE)
this.state.body.body = PrettyData.pd.xml(this.state.body.body);
else if(this.state.body.type == Util.responseTypes.CSS_TYPE)
this.state.body.body = PrettyData.pd.css(this.state.body.body);
}catch(e){}

bodyContent = (React.createElement("pre", {className: "resBodyContent"}, this.state.body.body));
}
}else{
bodyContent = null;
this.loadBody();
}

if(this.props.data.resHeader){
for(var key in this.props.data.resHeader){
resHeaderSection.push(React.createElement("li", {key: "resHeader_" + key}, React.createElement("strong", null, key), " : ", this.props.data.resHeader[key]))
}
}
}

detailSection = (
React.createElement("div", null,
React.createElement("section", {className: "resHeader"},
React.createElement("h4", {className: "subTitle"}, "response header"),
React.createElement("div", {className: "detail"},
React.createElement("ul", {className: "uk-list"},
React.createElement("li", null, "HTTP/1.1 ", React.createElement("span", {className: "http_status http_status_" + this.props.data.statusCode}, this.props.data.statusCode)),
resHeaderSection
React.createElement("div", null,
React.createElement("section", {className: "resHeader"},
React.createElement("h4", {className: "subTitle"}, "response header"),
React.createElement("div", {className: "detail"},
React.createElement("ul", {className: "uk-list"},
React.createElement("li", null, "HTTP/1.1 ", React.createElement("span", {className: "http_status http_status_" + this.props.data.statusCode}, this.props.data.statusCode)),
resHeaderSection
)
)
),

React.createElement("section", {className: "resBody"},
React.createElement("h4", {className: "subTitle"}, "response body"),
React.createElement("div", {className: "detail"}, bodyContent)
)
),

React.createElement("section", {className: "resBody"},
React.createElement("h4", {className: "subTitle"}, "response body"),
React.createElement("div", {className: "detail"}, bodyContent)
)
)
);
);
}

return (
Expand Down
1,355 changes: 886 additions & 469 deletions web/page.js

Large diffs are not rendered by default.

52 changes: 33 additions & 19 deletions web/src/detailPanel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var PrettyData = require("../../node_modules/pretty-data");
var Util = require("../../lib/web_util");

function init(React){

var DetailPanel = React.createClass({
Expand Down Expand Up @@ -25,6 +28,7 @@ function init(React){
self.setState({
body : {
body : resObj.content,
type: resObj.type,
id : resObj.id
}
});
Expand Down Expand Up @@ -72,37 +76,47 @@ function init(React){
var imgEl = { __html : '<img src="'+ this.state.body.img +'" />'};
bodyContent = (<div dangerouslySetInnerHTML={imgEl}></div>);
}else{
try{
// trying to prettify, sometimes content-types are not right and that raises exceptions
if (this.state.body.type == Util.responseTypes.JSON_TYPE)
this.state.body.body = PrettyData.pd.json(this.state.body.body);
else if(this.state.body.type == Util.responseTypes.XML_TYPE || this.state.body.type == Util.responseTypes.HTML_TYPE)
this.state.body.body = PrettyData.pd.xml(this.state.body.body);
else if(this.state.body.type == Util.responseTypes.CSS_TYPE)
this.state.body.body = PrettyData.pd.css(this.state.body.body);
}catch(e){}

bodyContent = (<pre className="resBodyContent">{this.state.body.body}</pre>);
}
}else{
bodyContent = null;
this.loadBody();
}

if(this.props.data.resHeader){
for(var key in this.props.data.resHeader){
resHeaderSection.push(<li key={"resHeader_" + key}><strong>{key}</strong> : {this.props.data.resHeader[key]}</li>)
}
}
}

detailSection = (
<div>
<section className="resHeader">
<h4 className="subTitle">response header</h4>
<div className="detail">
<ul className="uk-list">
<li>HTTP/1.1 <span className={"http_status http_status_" + this.props.data.statusCode}>{this.props.data.statusCode}</span></li>
{resHeaderSection}
</ul>
</div>
</section>

<section className="resBody">
<h4 className="subTitle">response body</h4>
<div className="detail">{bodyContent}</div>
</section>
</div>
);
<div>
<section className="resHeader">
<h4 className="subTitle">response header</h4>
<div className="detail">
<ul className="uk-list">
<li>HTTP/1.1 <span className={"http_status http_status_" + this.props.data.statusCode}>{this.props.data.statusCode}</span></li>
{resHeaderSection}
</ul>
</div>
</section>
<section className="resBody">
<h4 className="subTitle">response body</h4>
<div className="detail">{bodyContent}</div>
</section>
</div>
);
}

return (
Expand Down