Skip to content

Commit

Permalink
add httperror rule
Browse files Browse the repository at this point in the history
  • Loading branch information
didierfred committed Dec 8, 2019
1 parent 37ad06b commit d09d40a
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 22 deletions.
61 changes: 41 additions & 20 deletions GreenPanel.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,21 @@
td {
border: 1px solid white;
overflow: hidden;
padding : 2.2pt;
text-overflow: ellipsis;
font-size: 10px;
text-align: left;
}

.center-td {
text-align: center;
}

table {
border-collapse: collapse;
table-layout: fixed;
width: 95%;
margin: auto;
}

.bestPracticeLink {
Expand All @@ -87,6 +94,17 @@
font-size: 10px;
line-height: 5px;
}

.line {
width: 90%;
margin: auto;
color: rgba(0, 0, 0, 0);
line-height: 1px;
background-color: rgb(216, 221, 215);
margin-top: 1px;
margin-bottom: 10px;
}

</style>

</head>
Expand All @@ -103,50 +121,52 @@

<br />

<div data-i18n="activateBestPracticeAnalysisCheckbox">
<input type="checkbox" class="btn btn-primary btn-sm" id="analyseBestPracticesCheckBox"></input>
<div data-i18n="activateBestPracticeAnalysisCheckbox" style="font-size:10pt;">
<input type="checkbox" id="analyseBestPracticesCheckBox"></input>
</div>


</div>
<script type="text/javascript" data-main="script/utils.js" src="script/require.js"> </script>


<div id="ecoIndexView" hidden>

<H4> EcoIndex <span id="grade"></span>
</H4>

<table class="table table-condensed">
<table class="mytable">
<tbody>
<tr>
<th data-i18n="ecoIndex"></th>
<th data-i18n="waterConsumption"></th>
<th data-i18n="greenhouseGasesEmission"> </th>
<th data-i18n="ecoIndex" class="center-td"></th>
<th data-i18n="waterConsumption" class="center-td"></th>
<th data-i18n="greenhouseGasesEmission" class="center-td"> </th>
</tr>
<tr>
<td id="ecoIndex"></td>
<td id="waterConsumption"></td>
<td id="greenhouseGasesEmission"></td>
<td id="ecoIndex" class="center-td"></td>
<td id="waterConsumption" class="center-td"></td>
<td id="greenhouseGasesEmission" class="center-td" ></td>
</tr>
</tbody>
</table>
<div class="line">.</div>


<table class="table table-condensed">
<table>
<tbody>
<tr>
<th data-i18n="requestNumber"> </th>
<th data-i18n="pageSize"> </th>
<th data-i18n="domSize"> </th>
<th data-i18n="requestNumber" class="center-td"> </th>
<th data-i18n="pageSize" class="center-td"> </th>
<th data-i18n="domSize" class="center-td"> </th>
</tr>
<tr>
<td id="requestNumber"></td>
<td id="responsesSize"></td>
<td id="domSize"></td>
<td id="requestNumber" class="center-td"></td>
<td id="responsesSize" class="center-td"></td>
<td id="domSize" class="center-td"></td>
</tr>
</tbody>
</table>
<div class="line"> </div>
</div>

<div id="bestPracticesView" hidden>
<H4 data-i18n="bestPractices"></H4>

Expand All @@ -159,15 +179,15 @@ <H4 data-i18n="bestPractices"></H4>
<script src="script/externalLibs/require.js"> </script>
<script src="script/utils.js"> </script>
<script src="script/rulesManager.js"> </script>
<script src="script/rules.js"> </script>
<script src="script/rules/SocialNetworkButton.js"></script>
<script src="script/rules.js"> </script>
<script src="script/rules/AddExpiresOrCacheControlHeaders.js"></script>
<script src="script/rules/CompressHttp.js"></script>
<script src="script/rules/DomainsNumber.js"></script>
<script src="script/rules/DontResizeImageInBrowser.js"></script>
<script src="script/rules/EmptySrcTag.js"></script>
<script src="script/rules/ExternalizeCss.js"></script>
<script src="script/rules/ExternalizeJs.js"></script>
<script src="script/rules/HttpError.js"></script>
<script src="script/rules/HttpRequests.js"></script>
<script src="script/rules/ImageDownloadedNotDisplayed.js"></script>
<script src="script/rules/JsValidate.js"></script>
Expand All @@ -180,6 +200,7 @@ <H4 data-i18n="bestPractices"></H4>
<script src="script/rules/OptimizeSvg.js"> </script>
<script src="script/rules/Plugins.js"> </script>
<script src="script/rules/PrintStyleSheet.js"> </script>
<script src="script/rules/SocialNetworkButton.js"></script>
<script src="script/rules/StyleSheets.js"> </script>
<script src="script/rules/UseETags.js"> </script>
<script src="script/rules/UseStandardTypefaces.js"> </script>
Expand Down
17 changes: 17 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@
}
}
},
"rule_HttpError": {
"message": "Avoid HTTP request errors",
"description": "Avoid HTTP errors"
},
"rule_HttpError_DetailDescription": {
"message": "Requests with HTTP errors consume resources unnecessarily.",
"description": "Avoid HTTP errors"
},
"rule_HttpError_Comment": {
"message": "$NB_HTTP_ERRORS$ HTTP error(s)",
"description": "Number of http errors",
"placeholders": {
"NB_HTTP_ERRORS": {
"content": "$1"
}
}
},
"rule_HttpRequests": {
"message": "Limit the number of HTTP requests (<27)",
"description": "Limit the number of HTTP requests"
Expand Down
19 changes: 18 additions & 1 deletion _locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@
}
}
},
"rule_HttpError": {
"message": "Eviter les requêtes en erreur",
"description": "Avoid HTTP errors"
},
"rule_HttpError_DetailDescription": {
"message": "Les requêtes en erreurs consomment inutilement des ressources.",
"description": "Avoid HTTP errors"
},
"rule_HttpError_Comment": {
"message": "$NB_HTTP_ERRORS$ erreur(s) HTTP",
"description": "Number of http errors",
"placeholders": {
"NB_HTTP_ERRORS": {
"content": "$1"
}
}
},
"rule_HttpRequests": {
"message": "Limiter le nombre de requêtes HTTP (<27)",
"description": "Limit the number of HTTP requests"
Expand Down Expand Up @@ -586,7 +603,7 @@
"description": "Do not use standarts social button"
},
"rule_SocialNetworkButton_DefaultComment": {
"message": "Pas de bouton standard de social network trouvé",
"message": "Pas de bouton standard de réseau sociale trouvé",
"description": "No standart social network button found "
},
"rule_SocialNetworkButton_Comment": {
Expand Down
20 changes: 20 additions & 0 deletions script/rules/HttpError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
rulesManager.registerRule({
complianceLevel: 'A',
id: "HttpError",
comment: "",
detailComment: "",

check: function (measures) {
let errorNumber = 0;
if (measures.entries.length) measures.entries.forEach(entry => {
if (entry.response) {
if (entry.response.status >=400 ) {
this.detailComment += entry.response.status + " " + entry.request.url + "<br>";
errorNumber++;
}
}
});
if (errorNumber > 0) this.complianceLevel = 'C';
this.comment = chrome.i18n.getMessage("rule_HttpError_Comment", String(errorNumber));
}
}, "harReceived");
4 changes: 3 additions & 1 deletion script/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ function getOfficialSocialButtonFormUrl(url)
if (url.includes("platform.linkedin.com/in.js")) return "linkedin";
if (url.includes("assets.pinterest.com/js/pinit.js")) return "pinterest";
if (url.includes("connect.facebook.net") && url.includes("sdk.js")) return "facebook";
if (url.includes("platform-api.sharethis.com/js/sharethis.js")) return "sharethis.com (mutliple social network) ";
if (url.includes("s7.addthis.com/js/300/addthis_widget.js")) return "addthis.com (mutliple social network) ";
if (url.includes("static.addtoany.com/menu/page.js")) return "addtoany.com (mutliple social network) ";
return "";
}

Expand All @@ -361,4 +364,3 @@ function debug(lazyString) {
const message = typeof lazyString === 'function' ? lazyString() : lazyString;
console.log(`GreenIT-Analysis [DEBUG] ${message}\n`);
}

1 change: 1 addition & 0 deletions tests/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<script src="../script/rules/EmptySrcTag.js"> </script>
<script src="../script/rules/ExternalizeCss.js"> </script>
<script src="../script/rules/ExternalizeJs.js"> </script>
<script src="../script/rules/HttpError.js"> </script>
<script src="../script/rules/HttpRequests.js"> </script>
<script src="../script/rules/ImageDownloadedNotDisplayed.js"> </script>
<script src="../script/rules/JsValidate.js"> </script>
Expand Down
90 changes: 90 additions & 0 deletions tests/spec/Rules_Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,96 @@ describe("rules.js", function () {
});
});

describe("#HttpErrorRule", function () {

beforeEach(function () {
});

it(" 2 http requests with no error, it should return A", function () {
let rulesChecker = rulesManager.getNewRulesChecker();
const measures = {
entries:
[{
request: { url: "test" },
response:
{
status: 301, statusText: "", httpVersion: "http/2.0", headers:
[{ name: "content-encoding", value: "gzip" }]
}
},
{
request: { url: "test2" },
response:
{
status: 200, statusText: "", httpVersion: "http/2.0", headers:
[{ name: "content-encoding", value: "gzip" }]
}
}]
};
let rule = rulesChecker.getRule("HttpError");
rule.check(measures);
expect(rule.complianceLevel).toEqual('A');
});

it(" 2 http requests with one error, it should return C", function () {
let rulesChecker = rulesManager.getNewRulesChecker();
const measures = {
entries:
[{
request: { url: "test" },
response:
{
status: 404, statusText: "", httpVersion: "http/2.0", headers:
[{ name: "content-encoding", value: "gzip" }]
}
},
{
request: { url: "test2" },
response:
{
status: 200, statusText: "", httpVersion: "http/2.0", headers:
[{ name: "content-encoding", value: "gzip" }]
}
}]
};
let rule = rulesChecker.getRule("HttpError");
rule.check(measures);
expect(rule.complianceLevel).toEqual('C');
});


it(" 2 http requests with one error, it should return C", function () {
let rulesChecker = rulesManager.getNewRulesChecker();
const measures = {
entries:
[{
request: { url: "test" },
response:
{
status: 500, statusText: "", httpVersion: "http/2.0", headers:
[{ name: "content-encoding", value: "gzip" }]
}
},
{
request: { url: "test2" },
response:
{
status: 200, statusText: "", httpVersion: "http/2.0", headers:
[{ name: "content-encoding", value: "gzip" }]
}
}]
};
let rule = rulesChecker.getRule("HttpError");
rule.check(measures);
expect(rule.complianceLevel).toEqual('C');
});

afterEach(function () {
});
});



describe("#HttpRequestsRule", function () {

beforeEach(function () {
Expand Down

0 comments on commit d09d40a

Please sign in to comment.